odnoklassniki / one-nio

Unconventional I/O library for Java
Apache License 2.0
655 stars 97 forks source link

Selector.wakeup missing #24

Closed pveentjer closed 6 years ago

pveentjer commented 6 years ago

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.

apangin commented 6 years ago

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.

pveentjer commented 6 years ago

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.