Deregister a key immediately?
Alan.Bateman at oracle.com
Fri Jun 13 09:25:06 UTC 2014
On 13/06/2014 10:07, Wang Weijun wrote:
> I am using NIO Selector to monitor several channels. Whenever an exception is thrown on one channel I call key.cancel(). At the beginning of the loop I also call selector.keys.isEmpty() to see if there are channels still working. However, it seems key.cancel() does not automatically remove it from selector.keys() so I add a dummy selector.select(1) after the cancel call. Until now I didn't find it harmful but is there another way to deregister the key without calling select()?
No, you have to select (or selectNow) in order to flush the key.
I don't I understand your comment about using isEmpty "to see if there
are channels still working", maybe you could expand on that. Also when
you say you get an exception then if this is an IOException (connection
reset for example) then I would think the best thing is to just close
the channel. The Selector will clean-up on the next select.
More information about the nio-dev