Why? Reading is not blocked. Do it in the current thread. So you have endless problems. You will have to unregister OP_READ before moving on to the read stream, which is simple enough, but the tricky part is that when the read stream completes the read, it will have to reregister OP_READ, which requires either (i) the wakeup () selector, because why the selected thread starts when there is nothing to do, which is wasteful, or (ii) use the queue of pending re-registrations, which delays the next reading on this channel, until after the next selector selection it wakes up, which is also a waste iterative, otherwise you need to immediately activate the selector when adding to the queue, which is also wasteful if nothing is ready. I have never seen a compelling NIO architecture that used a variety of themes to select and read.
Do not do that. If you must have multithreading, organize your channels into groups, each of which has its own selector and its own stream, and all these streams perform their own reading.
Similarly, there is no need to write in a separate thread. Just write when you have something to write.
source share