java nio selector.select(),尽管通道已准备就绪,Java NIO Selector select()返回0

本文围绕Java NIO Selector的select()方法展开,当select()返回0时,通常认为是被唤醒或线程中断,但实际中存在有就绪通道时select()仍返回0的情况。通过修改代码遍历就绪键集的方式解决了问题,还指出foreach遍历集合修改元素可能导致异常,建议使用迭代器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

My Java NIO Selector is implemented using select() so it blocks until any of these occur:

a registered channel is ready

it is wakeup()'ed

the thread is interrupted

From this, I made a few assumptions about the case where select() returns 0:

it must've been reason 2. or 3.

selectedKeys() should return an empty ResultSet

I don't need to call selectedKeys() and can continue to the next loop iteration where select() will be called again

However, I encountered situations where select() returned 0 although there is a ready channel. selectedKeys() returns a Set with 1 SelectionKey as expected.

Even several calls to select() would always return 0 until the channel was processed and the SelectionKey was removed. This situation basically ends up in an endless loop as select() does not block but always instantly return 0.

Simplified code:

Selector selector = Selector.open();

SocketChannel channel;

for (...) { // for each node

// Create and connect channels...

...

channel.configureBlocking(false);

channel.register(selector, SelectionKey.OP_READ, someRelatedObject);

}

int ready;

Set readyKeys;

while (true) {

ready = selector.select();

readyKeys = selector.selectedKeys();

System.out.println("Ready channels: " + ready);

System.out.println("Selected channels: " + readyKeys.size());

if (ready == 0) {

continue;

}

for (SelectionKey key : readyKeys) {

if (key.isValid() && key.isReadable()) {

// Take action...

}

readyKeys.remove(key);

}

}

Why does select() return 0 although there is a ready channel?

What is the suggested way of dealing with this?

EDIT:

Changing this:

for (SelectionKey key : readyKeys) {

if (key.isValid() && key.isReadable()) {

// Take action...

}

readyKeys.remove(key);

}

to this

for (SelectionKey key : readyKeys) {

readyKeys.remove(key);

if (key.isValid() && key.isReadable()) {

// Take action...

}

}

solved the problem. In some cases, the code would continue the for loop before remove()ing the key.

EDIT 2:

I just recently learned that my foreach loop over the selected keys set is bad. foreach uses the set's iterator. Modifying a collection directly (not via the iterator's methods) while iterating over it may result in "arbitrary, undeterministic" behavior.

The selected keys set may provide a fail-fast iterator. Fail-fast iterators detect such modifications and throw a ConcurrentModificationException upon the next iteration. So modifying the set in a foreach either risks undeterministic behavior or may cause exceptions - depending on the iterator implementation.

Solution: don't use foreach. Use the iterator and remove the key via iterator.remove().

Iterator iterator;

SelectionKey key;

while (true) {

// ...

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

while (iterator.hasNext()) {

key = iterator.next();

iterator.remove();

// ...

}

}

解决方案

select() returns the number of keys that have changed. So if a key was already ready before the select() call then it could return 0 but selectedKeys could be non-empty.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值