开发者

What is the default behaviour of a selector when the key set is sized 0?

Just wondering what the normal behaviour of a Selector in Java is when the key set is 0, since it's essentially a pointless operation (no keys available for selection). It might be that the operation happens anyway, even with a key set of 0. However, is it then possible for a channel key to be开发者_运维知识库 registered during selection and for the operation to be re-evaluated during selection?

If this isn't the case, then surely a selection operation with no keys would take an infinite amount of time? And in this case what happens to the other networking code? :) Maybe it's just a case of making sure that the key set is never 0. But I'd like to know since I've written a selector and need to make sure that it matches the official behaviour.


The Selector class is abstract and the actual operation is dependent upon what kind of Selector implementation the SelectorProvider actually returns. These depend upon the operating system.

If the implementation uses Sun's sun.nio.ch.SelectorImpl as a base class (it too is abstract), then one can observe that the the select() methods synchronize on the public keys before invoking the abstract doSelect() method. The register() method also synchronizes on the public keys and therefore it is certainly possible that the register will have to wait for the select and the select will never complete because nothing is registered.

Unforunately it is all abstract classes so we cannot be sure. I do observe that the Selector documentation describes how to unstick a thread stuck in select() so presumably it is something that comes up often enough to be worth documenting, and therefore you are right to worry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜