简体   繁体   English

如何控制连接到RMI服务器的多个客户端线程

[英]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 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? 那么我怎样才能控制这些客户端线程,以便我可以让我的客户端1等到客户端2出现然后执行他们需要执行的任何操作?

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. 我同意@EJP这是一个非常奇怪的要求。 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 您可以使用CountDownLatch实现此目的

Have a static CountDownLatch set to 1 将静态CountDownLatch设置为1

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

Client 2 counts it down 客户端2将其计算在内

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

Client 1 waits on it 客户端1等待它

    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 在实际代码中,你肯定希望有一些超时,所以如果client2没有显示你的应用程序不会永远挂起

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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