Search code examples
javaniosocketchannel

Further cleanup after closing a socket channel


Is there need to do further cleanup after closing a socket channel if the channel was used with a selector and the selector is still being used?


Solution

  • No. Closing the channel cancels the selection key. It will be removed from the key set on the next select().

    However, when the current select() returns, it is possible for a cancelled key to be in the selected-keys set, which you can detect via SelectionKey.isValid(), as seen in any decent NIO select loop example.