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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:43:21+00:00 2026-05-13T08:43:21+00:00

My program is a client connected to multiple servers. I save connection objects to

  • 0

My program is a client connected to multiple servers. I save connection objects to all servers in a static map object:

server1 -> connection1
server2 -> connection2
serverN -> connectionN

public class CacheConnection {

    private final static Map cacheConnection = new HashMap();

    public static void add(String serverName, Socket sock) {
        synchronized (cacheConnection) {
            cacheConnection.put(serverName, sock);
        }
    }

    public static Socket get(String serverName) {
        return (Socket) cacheConnection.get(serverName);
    }

    ..
}

I have many threads getting connections from this map to communicate with the servers. How can I ensure a connection can only be used by one thread at a time?

For example, I want to be sure thread 1 and thread 2 cannot use connection 1 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-05-13T08:43:21+00:00Added an answer on May 13, 2026 at 8:43 am

    I am not completely sure, what you want. I assume that you want to guarantee that only one thread at a time accesses one particular server.

    If your connection is something like a socket, then you can use it as a lock in a synchronization statement:

    private void send(Connection c, Data d) {
      synchronized (c) {
        // for each connection object, only one thread may be inside this block.
        // all other threads wait until the thread currently in this block exits it.
        c.send(d);
      }
    }
    
    // somewhere else ...
    
    Data data = determineDataToSend()
    Connection connection = map.get(key);
    send(connection, data)
    

    You can put the logic also into a decorator for the connection. This is especially useful if your connection has more than one method that send or receive (e.g., because you use a higher abstraction level like RMI):

    public interface PowerfulConnection {
      public void doA();
      public int doB(ParameterForB param);
    }
    
    public class ConnectionImpl implements PowerfulConnection {
       // handles the actual connection
    }
    
    /**
     * This method is a decorator for PowerfulConnection that synchronizes all method accesses.
     */
    public class SynchronizedConnection implements PowerfulConnection {
      private PowerfulConnection target;
    
      public SynchronizedConnection(PowerfulConnection target) {
        if (target == null) throw new NullPointerException();
        this.target = target;
      }
    
      public synchronized void doA() {
        target.doA();
      }
    
      public synchronized int doB(ParameterForB param) {
        return target.doB(param);
      }
    }
    

    If you are using the decorator approach, then the only thing you need to change is the instance creation. Instead of:

    private void connect(key, connectionParams) {
      map.put(key, new ConnectionImpl(connectionParams));
    }
    

    use

    private void connect(key, connectionParams) {
      map.put(key, new SynchronizedConnection(new ConnectionImpl(connectionParams)));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my client program, there is a WCF connection that is opened at startup
I have a Client program by tcp connection that send data to a server.
I'm trying to implement a server-client socket program in Java that can support multiple
I'm using TCP socket connetion between a server program and a client program. Multiple
I'm using TCP socket connetion between a server program and a client program. Multiple
I have written an ActionScript client program that tries to connect to a local
I have a client program written in .NET 2.0, that connects to the web
I have a file transfer program. The program (Client) does following operations to send
I have a client program that consumes a web service. It works quite well
I want to run my client program 25,000 times. I need to create a

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.