java rmi死锁 [英] java rmi deadlock
问题描述
我刚开始使用Java rmi编程,我在代码中遇到以下问题:
I just have started to program with java rmi and I face the following problem in my code:
我的服务器有两个Remote方法,通常实现如下:
My server has two Remote methods which are implemented in general as follows:
public class ServerImpl extends UnicastRemoteObject implements Server{
....
Synchronized void foo(){ aClient.Foo3();}
Synchronized void foo1(){ .... }
}
我的客户有一个远程方法,实现如下:
My clients have one remote method which is implemented as follows:
public class ClientImpl extends UnicastRemoteObject implements Client{
....
void Foo3(){theServer.foo1();}
}
因此,当aClient调用服务器的foo()时,服务器调用客户端的Foo3(),然后aClient想要调用服务器的foo1()时,就会出现死锁(服务器和客户端都不会继续前进) 。我知道这是由于Synchronized关键字引起的。问题在于这些方法必须同步(我不希望同时有两个线程),而且我对如何解决此问题丝毫不了解。任何帮助表示赞赏。
So when aClient calls server's foo(), the server calls client's Foo3() and then aClient wants to call server's foo1() and we have a deadlock (neither the server nor the client moves on). I know that this is caused because of the Synchronized keyword. The problem is that these methods have to be Synchronized (I don't want two threads the same time in there), and I don't have the slightest idea of how to resolve this issue. Any help appreciated.
非常感谢!
推荐答案
您可以使用同步每个方法中具有不同锁定对象的块。 同步
方法将锁定在此
上,因此一次只能访问一个。
You can use a synchronized block with different locking object inside each method. synchronized
methods lock on this
so only one may be visited at a time.
这篇关于java rmi死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!