How should the writing to a non-blocking nio channel be handled?
if (key.isAcceptable()) {
~ codes ~
client.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} else if (key.isConnectable()) {
~ codes ~
client.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
} else if (key.isReadable()) {
~ codes ~
} else if (key.isWritable()) {
~ codes ~
<== writes data to the channel here ==>
}
Apperently if I use that the select will keep returning, therefore eating up the resources.
According to this page http://www-128.ibm.com/developerworks/java/library/j-perf03174.html, [quote]if the socket is registered with the Selector in WRITE mode, then the Selector.select() call will always return immediately, because it has at least one socket which is ready to be written to.
[/quote]
Now my problem, since calling register() function in a thread other than the one that calls select() will cause blocking problem. How would I write data to the channel if I can’t register the channel to write state?