Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:46:36+00:00 2026-06-01T04:46:36+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:46:38+00:00Added an answer on June 1, 2026 at 4:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using java nio selector, and seem to hit the following issue randomly
using java.nio one has to register interest in operations via the SelectableChannel: SelectionKey =
I'm currently playing around with java.nio , which I haven't been using for a
I wrote a simple client/server in java that transfers files using java NIO Socketchannel.
I am writing a networking application using the java.nio api. My plan is to
i'm working with java.nio.channels.Selector and i'd like to create a separate thread for each
In my program using java nio, the socketchannel.write() becomes very slow when it tries
I would like to write an asynchronous server using Java 7 and NIO 2.
I have a java.nio.MappedByteBuffer that I am using to read integers from a little-endian
I've just started using java.nio and probably used it some wrong way so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.