在Java中是否有等效的Linux epoll?
Is there an equivalent of Linux epoll in Java?
epoll允许线程对许多异类事件作出反应.例如,我可以有一个对套接字事件或控制台输入作出反应的线程.在C ++中,我可以通过向epoll注册stdio和套接字来实现此目的.我的线程会被其中任何一个事件触发.
epoll allows a thread to react to a number of heterogenous events. For instance, I can have a thread that reacts to either a socket event or an input from the console. In C++ I can implement this by registering stdio and the socket with epoll. My thread will be triggered by an event from either if these.
Java中是否有类似的功能?我看了看nio包,它允许我用选择器注册许多套接字.但是似乎并没有向选择器注册控制台/标准io的方法.我没看到什么吗?还有另一种方法吗?
Is there a similar facility in Java? I looked at the nio package, it allows me to register a number of sockets with a selector. But there does not seem to be away to register the console / standard io with a selector. Am I not seeing something? Is there another way to do this?
关于为什么":我想编写一个通过套接字通信的程序,并且我想通过从控制台输入命令来驱动该程序.我知道如何通过将控制台输入和与不同线程的通信分开来完成此操作,但我很好奇是否有一种方法可以在单个线程中执行此操作.
On the "why": I want to write a program that communicates via sockets, and i want to drive this program by entering commands from console. I know how this can be done by separating console input and the communications to different threads, but i am curious whether there is a way to do this in a single thread.
谢谢. df
推荐答案Java SE 6中的增强功能
Enhancements in Java SE 6
java.nio
包含基于Linux epoll事件通知工具的新的java.nio.channels.SelectorProvider实现. epoll工具在Linux 2.6和更高版本的内核中可用.当有成千上万的Selector注册到Selector时,新的基于epoll的SelectorProvider实现比传统的基于轮询的SelectorProvider实现更具可伸缩性.当检测到2.6内核时,默认情况下将使用新的SelectorProvider实现.当检测到2.6之前的内核时,将使用基于轮询的SelectorProvider.
A new java.nio.channels.SelectorProvider implementation that is based on the Linux epoll event notification facility is included. The epoll facility is available in the Linux 2.6, and newer, kernels. The new epoll-based SelectorProvider implementation is more scalable than the traditional poll-based SelectorProvider implementation when there are thousands of SelectableChannels registered with a Selector. The new SelectorProvider implementation will be used by default when the 2.6 kernel is detected. The poll-based SelectorProvider will be used when a pre-2.6 kernel is detected.
docs.oracle /javase/8/docs/technotes/guides/io/enhancements.html
更多推荐
Java中是否有等效的epoll?
发布评论