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

  • Home
  • SEARCH
  • 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 6188639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:16:07+00:00 2026-05-24T02:16:07+00:00

I have a client-server architecture where the server has its clients do work in

  • 0

I have a client-server architecture where the server has its clients do work in turns, passing a parameter each time a client is supposed to do work. The client does work with the parameter, and when it is done, the parameter becomes “invalid” and can no longer be used to do work.

I’d like to avoid running the garbage collector while this is going on, and so I’m avoiding object allocations. So the plan is that the server will associate a single parameter object with each client, and will always pass the same parameter every time the client is asked to do work. However, this creates the problem that the parameter will have to be re-set to “valid” while also ensuring that the client (who may have kept a reference to the parameter from the last time around) can’t start using it (say, in a different thread) before it is asked to begin doing work.

So all of the parameter’s public methods are synchronized, and the “valid” state is set, followed by a (synchronous) beginWork call to the client, inside a synchronized block. But this creates the problem that the client will unknowingly hold the parameter’s lock, which could cause problems if the client wants to split its work into multiple threads. So I introduced a single-threaded ExecutorService which the server uses to fork off the call to beginWork, which ensures that the server will release the lock promptly. But this seems like a bad design to me — why should this class need a whole other thread?

So my question is: given everything I’ve just laid out, have I made some horrible design mistake that has caused me to overcomplicate this class, or does it really need to be this complex?

interface Client {
    public void doWork(Param p);
}

interface Param {
    public boolean isValid();
}

class Server {
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private final MyParam[] params;

    private class MyParam implements Param {
        boolean isValid;
        Client client;
        Runnable task = new Runnable() {
            @Override
            public void run() {
                client.doWork(MyParam.this);
            }
        }

        @Override
        public synchronized boolean isValid() {
            return isValid;
        }
    }

    public void runClients() {
        while (true) {
            for (MyParam param : params) {
                synchronized(param) {
                    param.isValid = true;
                    // fork the client so we release the lock promptly (ugly!)
                    executor.execute(param.task);
                }

                // ... wait for the client to finish ...
            }
        }
    }
}
  • 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-24T02:16:08+00:00Added an answer on May 24, 2026 at 2:16 am

    I fixed this by adding an explicit ReentrantLock to the Param interface. So instead of using a synchronized block, I just lock the param, and let the client unlock it if he wants to.

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

Sidebar

Related Questions

I have the following (simplified) architecture: client(s) --> bouncer --> server The clients send
I have a client/server application that communicates with .Net remoting. I need my clients
I have a client-server application that uses .net remoting. The clients are in a
I have a client/server application. One of the clients is a CLI. The CLI
I have to implement a Client/Server architecture in Java, where the Server may change
I'm starting to learn Groovy with a Client-Server architecture. As the server I have
I have a server-client architecture where the client sends an XML to the server
I have client and server programs which now communicate via TCP. I'm trying out
We have a client/server application with a rich client front end (in .Net) and
I have a client server application that sends XML over TCP/IP from client to

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.