NIO 选择器默认使用事件通知吗?

Chr*_*rth 1 java events nio kqueue

我发现这篇文章说 OpenJDK 在 Linux 上使用 epoll,但是有人知道 Selector 实现是否使用 kqueue、dev/poll 等而不是传统的轮询(如果可用)?

Sch*_*jer 5

是的,它确实。java.nio.channels.Selector.open() 根据您的底层操作系统返回不同的实现。例如。如果您使用来自 sun/oracle 的 JRE,您将得到:

Windows 
sun.nio.ch.WindowsSelectorImpl

Mac OS 
sun.nio.ch.KQueueSelectorImpl

Linux 
sun.nio.ch.EPollSelectorImpl

Solaris
sun.nio.ch.PollSelectorImpl
Run Code Online (Sandbox Code Playgroud)