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 8169755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:56:49+00:00 2026-06-06T20:56:49+00:00

I am implementing a simple file server using Java NIO with one selecting thread

  • 0

I am implementing a simple file server using Java NIO with one selecting thread and multiple worker threads(for performing real read/write).

The main part of the code looks like the following:

while (true) {
    int num = selector.select();
    if (num > 0) { 
        Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
        final SelectionKey key = keys.next();
        keys.remove();

        if (key.isValid()) {
            if (key.isAcceptable()) {
                accept(key);
            } else if (key.isReadable()) {
                performReadInWorkerThread (key);
            } else if (key.isWritable()) {
                performWriteInWorkerThread (key);
            }
        }
    }
}

As you can see from the code snippet, when a readable/writable channel is selected, I offload the read/write from the selecting thread to a worker thread.

Now the problem is that when a readble channel is handed over to the worker thread, and before it finishes/starts reading from the channel, the selecting thread loops again, and selector.select() selects the previously selected readable channel(because there’s still input buffer in the channel that is not yet fully consumed by the previously assigned worker thread), so again the channel is handed over to another worker thread, resulting in multiple worker threads reading the same channel.

I believe this is a design problem. My question is how I can ensure only one thread reading a channel at the same time?

  • 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-06T20:56:51+00:00Added an answer on June 6, 2026 at 8:56 pm

    Why? The read won’t block. Do it in the current thread. You are just in for endless problems this way. You will have to deregister OP_READ before you hand over to the read thread, which is easy enough, but the hard part is that when the read thread completes the read it will have to re-register OP_READ, which requires either (i) a selector wakeup(), which causes the select thread to run when there is possibly nothing to do, which is wasteful, or else (ii) use a queue of pending reregistrations, which delays the next read on that channel until after the next time the selector wakes up, which is also wasteful, or else you have to wakeup the selector immediately on adding to the queue, which is also wasteful if nothing is ready. I’ve never seen a convincing NIO architecture that used different select and read threads.

    Don’t do this. If you must have multithreading, organize your channels into groups, each with its own selector and its own thread, and have all those threads do their own reading.

    Similarly there is no need to write in a separate thread. Just write when you have something to write.

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

Sidebar

Related Questions

Imagine two simple java applications. Both of them are implementing the same JAR file
I am implementing both server and client side of a simple file download program.
I'm implementing a simple threaded application in which I have a server thread, and
I'm working on implementing a reasonably simple XML serializer/deserializer (log file parser) application in
Anyone knows a simple JavaScript library implementing the UNZIP algorithm? No disk-file access, only
I am implementing simple thread that passes messages to main UI thread to make
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I'm implementing a simple chat in .NET using Rx on the basis of this
I am implementing the simple JNI example , as given in http://java.sun.com/docs/books/jni/html/start.html At the
Basically, I would like a simple, easy, one-file way to parse an INI file

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.