In the process of converting our Java code, I use NIO, but I'm not sure how to design it.
My initial approach was to create a selector thread pool. If necessary, streams are started / destroyed, and channels are registered in the selector stream when they are connected / received cyclically. From there, each thread blocks select (), and when woken up starts the corresponding callback associated with each channel that has the selected key.
In addition to this "multiple selector stream" design, I also saw people say that they use the same selector stream and send stream pool. When the I / O operation is ready for execution, the selector notifies the dispatcher thread, which then processes the request. This model has the advantage of not blocking the I / O stream, but now we force all the IOs into one stream and are engaged in the synchronization / queue of events in the dispatcher.
In addition, I could not use one direct byte buffer to read each channel, passing it directly to the callback. Instead, I would have to copy the data every time a read occurs in the array and reset. (I think..)
What is the best way to implement this?
java nio
Stephen pape
source share