java Java中是否有等效的epoll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10621783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:52:24  来源:igfitidea点击:

Is there epoll equivalent in Java?

javamultithreadingepoll

提问by dfreit

Is there an equivalent of Linux epoll in Java?

Java 中是否有等效的 Linux epoll?

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.

epoll 允许一个线程对许多异类事件做出反应。例如,我可以有一个线程对套接字事件或来自控制台的输入做出反应。在 C++ 中,我可以通过使用 epoll 注册 stdio 和套接字来实现这一点。如果这些,我的线程将被来自任一事件的事件触发。

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?

Java 中是否有类似的功能?我查看了 nio 包,它允许我使用选择器注册多个套接字。但是似乎没有办法用选择器注册控制台/标准 io。我没有看到什么吗?有没有其他方法可以做到这一点?

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.

关于“为什么”:我想编写一个通过套接字通信的程序,我想通过从控制台输入命令来驱动这个程序。我知道如何通过将控制台输入和与不同线程的通信分开来实现这一点,但我很好奇是否有办法在单个线程中做到这一点。

Thanks. df

谢谢。df

回答by lslab

Enhancements in Java SE 6

Java SE 6 中的增强功能

java.nio

java.nio

A new java.nio.channels.SelectorProviderimplementation 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.

包括一个java.nio.channels.SelectorProvider基于 Linux epoll 事件通知工具的新实现。epoll 工具在 Linux 2.6 和更新的内核中可用。当有数千个 SelectableChannel 注册到 Selector 时,新的基于 epoll 的 SelectorProvider 实现比传统的基于轮询的 SelectorProvider 实现更具可扩展性。当检测到 2.6 内核时,将默认使用新的 SelectorProvider 实现。当检测到 2.6 之前的内核时,将使用基于轮询的 SelectorProvider。

https://docs.oracle.com/javase/8/docs/technotes/guides/io/enhancements.html

https://docs.oracle.com/javase/8/docs/technotes/guides/io/enhancements.html

回答by Micha? Kosmulski

Yes, the niopackage allows the use of Selectors which supply the functionality equivalent of poll()/select()and actually one of the implementations uses epollas the backend (this is selected via java.nio.channels.spi.SelectorProviderJava property). Selectors are usually used with network sockets, but if you look through the different Channelimplementations in the docs, I think it's likely you will be able to use this mechanism with standard input as well (there are helper classes which allow moving between old Stream-based APIs and the nioAPIs to some degree).

是的,该nio包允许使用Selectors 提供等效于poll()/的功能select(),实际上其中一个实现epoll用作后端(这是通过java.nio.channels.spi.SelectorProviderJava 属性选择的)。选择器通常与网络套接字一起使用,但是如果您查看Channel文档中的不同实现,我认为您很可能也可以将这种机制与标准输入一起使用(有一些帮助类允许在旧的Stream基于 API之间移动)以及nio一定程度上的API)。