24

Is there an equivalent of Linux epoll in Java?

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.

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.

Thanks. df

dfreit
  • 241
  • 1
  • 2
  • 5
  • 4
    Can you take a step back and edit your post to explain _why_ you want to do this? – Gray May 16 '12 at 15:30
  • I guess standard 'console input' would probably be `System.in` (although you should inject the reference, not hard-code the use). However, it doesn't look like it generates events (ie - there's no `register()` or similar method), so I don't think there's default behaviour like you want. You'd have to write (or find) something to provide this behaviour. – Clockwork-Muse May 16 '12 at 16:07
  • Related to http://stackoverflow.com/questions/1915262/java-use-nio-with-system-in and http://stackoverflow.com/questions/765981/how-to-get-selectablechannel-from-an-inputstream – Gray May 16 '12 at 18:00

2 Answers2

29

Enhancements in Java SE 6

java.nio

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.

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

Aron
  • 15,464
  • 3
  • 31
  • 64
lslab
  • 1,717
  • 1
  • 14
  • 5
  • Is this portable to non-linux Java implementations? Also, can you get a `SelectorProvider` on the console? – Gray Aug 28 '15 at 13:54
7

Yes, the nio package allows the use of Selectors which supply the functionality equivalent of poll()/select() and actually one of the implementations uses epoll as the backend (this is selected via java.nio.channels.spi.SelectorProvider Java property). Selectors are usually used with network sockets, but if you look through the different Channel implementations 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 nio APIs to some degree).

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
  • 4
    You can't. You can't register any channel with a `Selector` that isn't a `SelectableChannel`, and the only `SelectableChannels` are the socket channels and the pipe channels. – user207421 May 17 '12 at 03:26