odnoklassniki/one-nio

Selector.wakeup missing

pveentjer opened this issue · 2 comments

The java.nio.channels.Selector has a wakeup method so that externally the thread waiting on the selector can be notified. E.g. it can be used to signal the io thread to start sending some payload over the network.

The one.nio.net.Selector doesn't have a wakeup. This means that one-nio can't be used as a replacement for networking code that relies on the wakeup.

java.nio.channels.Selector is not MT-friendly - that's one of the main reasons why wakeup method is needed. E.g. in order to register a new socket you typically have to interrupt currently running select by calling wakeup and then call Channel.register on a selector thread.

one-nio Selector is much easier to use in multiple threads. You don't have to interrupt select in order to register/unregister a socket or to change listened events. It's possible to just call register/unregister/listen on any thread, and the changes will apply immediately, even if there is a select operation in progress on another thread. So generally wakeup is not required.

It's not difficult to implement wakeup, but I believe it's redundant in a well-designed architecture. If you want a selector thread to start writing, just call listen(session, Session.WRITEABLE), and the selector will immediately wake up if the socket you want to write into is indeed writeable.

Hi @apangin thanks for your answer. I'll give it a try and see if I can get one-nio up and running as replacement of the regular Java NIO networking.