简体   繁体   中英

How to get control over multiple client threads connecting to a RMI Server

Stolen from:

http://publib.boulder.ibm.com/infocenter/javasdk/v1r4m2/index.jsp?topic=%2Fcom.ibm.java.doc.diagnostics.142%2Fhtml%2Fid1418.html

"on the server side, when a client connects to the server socket, a new thread is forked to deal with the incoming call. "

so how can i get a control over these client threads so that i can make my client 1 wait till client 2 shows up and then perform whatever they need to perform?

Thank you.

You can't get control of the threads, but you don't need it. You have do control over the remote methods. Just put whatever synchronization, acting, semaphores etc. that you need inside your remote method implementations.

Having said that, it's a very strange requirement. Normally clients are independent of each other.

I agree with @EJP that this is a very strange requirement. The solution I will offer should work but is normally something you absolutely don't want to do, because it blocks a thread causing bad usability and scalability.

You can achieve this using a CountDownLatch

Have a static CountDownLatch set to 1

public class RmiEndPoint{
    static CountDownLatch startSignal = new CountDownLatch(1);

Client 2 counts it down

    public void executedByClient2(){
        SharedLock.countDown();
    }

Client 1 waits on it

    public void executedByClient1(){
        SharedLock.await();
        // do whatever you want to do
    }
}

In real code you definetly want to have some timeouts so your app doesn't hang for ever if client2 doesn't show up

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