Java NIO Selector select() returns 0 although chan

2020-08-17 06:22发布

问题:

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

  1. a registered channel is ready
  2. it is wakeup()'ed
  3. 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<SelectionKey> 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<SelectionKey> iterator;
SelectionKey key;
while (true) {
  // ...

  iterator = selector.selectedKeys().iterator();
  while (iterator.hasNext()) {
    key = iterator.next();
    iterator.remove();
    // ...
  }
}

回答1:

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.



回答2:

As you noted, it is because you didn't remove the selected key from the selected Set.

As we usually want to remove all selected keys after processing the Set, you can just call clear() on that Set after your loop, which can be a foreach or any type of loop you want:

Set<SelectionKey> readyKeys = selector.selectedKeys();
for (SelectionKey k : readyKeys) {
    // Process k
}
readyKeys.clear();

That way, you can use any type of loop you want ("regular" for or iterators) and be sure all ready-keys are removed, no matter what you do inside the for (including the problematic continue). Depending on what the iterator.remove() does internally, it might be more efficient to call clear() once instead of several iterator.remove() (though that would probably be micro-optimization).



标签: java nio