selected key removal

We often see the idiom:

Iterator itr = selector.selectedKeys().iterator();

while (itr.hasNext()){

SelectionKey key = (SelectionKey)itr.next();

itr.remove();

...// process the 'key'

...

}

Does this Iterator.remove() call remove the key from selector's selected key set or does it only remove the key from a copy of the set? In other words, should we have to call key.cancel() to really remove it from the selector's key set?

[603 byte] By [hiwaa] at [2007-11-26 17:10:13]
# 1

> Does this Iterator.remove() call remove the key from

> selector's selected key set

Yes. You have to do that because the selector just keeps adding to the selected set, it never clears it.

> should we have to call key.cancel() to really remove

> it from the selector's key set?

The key set is different from the selected set, so, yes, you have to call key.cancel(). There is no other way to do it.

ejpa at 2007-7-8 23:38:04 > top of Java-index,Core,Core APIs...