简体   繁体   中英

How to share 2 SSH connections in multithreaded environment

Currently I have a ExecuterService to which I submit multiple threads. A single SSH connection is shared among those threads. A thread acquires a lock on SSH connection (to execute some commands), remaining threads wait.

Now I want to optimize the perfomance and I want to use 2 SSH connections among those threads. To implement I consider that I will have to split my threads in 2 parts, and share the 2 connections among them. I am seeking if there is more appropriate way to do this. Thanks in advance for the responses.

I already have done POC concluding that, 2 SSH connections will work in parallel execution environment just fine.

If the time for one thread to use the SSH connection is constant, splitting the threads in 2 parts is acceptable. Else it may not be optimal, because you could have one part ending its processing before the other, and one of the connection would be available and not used.

On a theorical point of view you have a problem with a number of clients requiring access to multiple servers. Your description is having as many queues as servers, and each client waits on one queue. It is not stupid and is what is used in real world in supermarket checkouts. But there is enough intelligence in a human to change queue if they see that one queue is empty while their is not... The nice point is that it is trivial to implement.

Another possibility is to have a single queue and as soon as a server is available, it signals it to the dispatcher point that sends it the first client. This offers the best repartition in wait time. In real world, it is often used in administrative services with few counters. The bad point is that is slightly more complex to implement. It can be implemented with a queue for the clients, a queue (or a stack) for the available servers and a semaphore that blocks the next client until a server is available.

For trivial short tasks, the first way may be the best because the gain provided by the second algorithm may not be enough for the time lost due the the higher complexity. Else you still have the balance between the developpement (and maintenance) cost for a more complex algo against a less efficient but simpler one.

Create an ArrayBlockingQueue<SSH_connection> q . Let a thread, when it wants to communicate, do conn = q.take() , and when finished, q.put(conn) . At the beginning, put both connections to that queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM