What's the deference between SocketChannel.register()
and SelectionKey.interestOps()
?
In the echo NIO server found in this SO question,
for every key that is accepted, a register is called for a read op. Then once a message is read, the register is called again for a write op. However, after writing a message, rather than registering another read op it calls key.interestOps(SelectionKey.OP_READ)
.
Why the difference?
register()
will lose or change the key attachment, and possibly return a new SelectionKey
altogether: it isn't specified. Use interestOps()
in this situation.