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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:11:23+00:00 2026-05-26T13:11:23+00:00

I need to implement an application that is simultaneously communicating with multiple clients using

  • 0

I need to implement an application that is simultaneously communicating with multiple clients using a (bidirectional) request-response protocol. Previously I have implemented this using two dedicated threads for each client (one reader/reactor and one writer/initiator). The problem with this is that the thread management became quite complex and ugly. Is there any standard way of handling this, possibly even with just one thread, or at least a constant amount of threads for handling all of the clients?

This is sort of how some communication would look in a thread with the blocking implementation:

Command response = request("cmd1", "a", "b");
if(!response.is("OK")) {
    return;
}
response = request("cmd2", "c");
if(!response.is("OK")) {
    return;
}
response = request("cmd3");

Between sending the request and awaiting the corresponding response I would like for the current thread to be able to do other work, but then continue execution once the response actually arrives.

I know that it could be possible to use asynchronious IO and register Java Future/Runnable instances to run once the response for the request is received, but this easily turns into a multi-level nesting of anonymous Runnable subclasses and I suspect that it will be more pain then it is worth. This would probably result in something like the sample below, which quickly becomes highly nested and unreadable. Surely there must be an easier way?

request("cmd1", "a", "b", new ResponseHandler() {
    public void response(Command response) {
        if(response.is("OK")) {
            request("cmd2", "c", new ResponseHandler() {
                public void response(Command response) {
                    if(response.is("OK")) {
                        request("cmd3", new NullResponseHandler());
                    }
                }});
        }
    }});

I have also considered the possibility of using a dedicated Actor framework to handle the request-response logic. While they look like they could help in this situation I have never worked with such frameworks before so I do not know if they are appropriate for this situation.

In short, my question is: How do I handle an arbitrary amount of request-response connections in a non-blocking way so that a constant amount of threads is sufficient? Are Actor frameworks a valid approach?

PS. I am using Java for this project.

  • 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-05-26T13:11:23+00:00Added an answer on May 26, 2026 at 1:11 pm

    I think this should be quite simple using some Java NIO framework like e.g. netty. Haven’t used an Actor framework so no idea if that wouldn’t be a better fit though.

    Basically you create one class that handles the whole communication and stores the necessary information – the framework handles the whole threading in the background you just provide a method for e.g. messageReceived and handle it there.

    The downside is, that you have to basically write your own state machine which may not be that simple – but it’s certainly the easiest way to use NIO.

    Example:

    enum State {
        S0, S1
    }
    private State state = State.S0;
    public void messageReceived(
            ChannelHandlerContext ctx, MessageEvent e) {
        switch(state) {
        case S0:
            // read object from channel and write appropriate response
            e.getChannel().write("HELO");  // writes are asynchronous 
            state = State.S1;
            break;
        case S1:
            // same as S0
            e.getChannel().write("DONE");
            break;
        }
    }
    

    Note that reading the netty tutorial is still absolutely necessary, because there are things about NIO that aren’t self explaining (at least they weren’t for me). But there are several examples that are easy to work through which should teach you the basics.

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

Sidebar

Related Questions

I have a project (Web Application) that I need to implement with Web Forms.
I need to implement handling of redelivery of JMS messages in the application that
I need to implement a login feature for the web application that I am
I have a Cocoa application that displays an application-modal alert using the NSAlert class.
We need to implement a Java desktop application that allows us (initially) to create
I need to get all the objects in my application that implement a given
I have an MVVM kiosk application that I need to restart when it has
I need to implement a security framework for a multiuser application that is in
I need to implement a custom error page in my rails application that allows
I am developing one application and in that application I need to implement push

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.