Java RMI和线程同步问题 [英] Java RMI and Thread Synchronization questions

查看:142
本文介绍了Java RMI和线程同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上有两个关于Java RMI和线程同步的问题:

I actually have two questions about Java RMI and thread synchronization:

1)如果我将RMI远程方法实现为synchronized,它们是否保证互斥?我需要确保没有两个我的RMI方法(提供给客户端的方法)同时执行。

1) If I implement my RMI remote methods as synchronized, are they guaranteed to be mutually exclusive? I need to make sure that no two of my RMI methods (methods offered to clients) execute at the same time.

2)我有一个服务器定期执行的方法。它用于清理。当远程客户端运行/使用任何RMI方法时,我必须确保不执行此特定方法。此外,当该方法运行时,不应该进行RMI调用。即客户必须等待。知道我怎么能这样做吗?我已经读过关于Locks的内容,但我不知道如何在这种情况下使用它们。

2) I have a method that the server executes periodically. It is used to do cleanups. I have to make sure that this particular method does not execute when there is any RMI method being run/used by remote clients. Also, when that method is running, RMI calls shouldn't be possible. I.e. Clients must wait. Any idea how I can do that? I have read about Locks, but I do not know how to use them in this scenario.

我已经考虑将RMI方法实现为静态并在RMI中包含清理方法接口,但它似乎不是解决问题的优雅方式。

I have considered implementing RMI methods as static and including that cleanup method inside the RMI interface, but it does not seem to be an elegant way of solving the problem.

我还在RMI接口内部将清理方法编写为synchronized。当我运行它进行测试时,方法之间似乎没有冲突,但我无法确定。

I have also written the cleanup method inside the RMI interface as synchronized. When I ran it for testing, there did not seem to be collisions between methods, but I cannot be sure.

感谢您的时间和答案。

推荐答案


1)如果我将RMI远程
方法实现为同步,那么它们是
保证互相排斥? I
需要确保我的
RMI方法(提供给
客户端的方法)中没有两个同时执行。

1) If I implement my RMI remote methods as synchronized, are they guaranteed to be mutually exclusive? I need to make sure that no two of my RMI methods (methods offered to clients) execute at the same time.

RMI本身不提供此类保证(与EJB不同),并且可以同时执行对同一远程对象的两次调用,除非您实现某些同步。您的方法是正确的,并且同步所有方法确实将确保它们中没有两个同时在同一对象上运行。注意:单独的关键字 synchronized 相当于 synchronized(this)

RMI does not provide such guarantee on its own (unlike EJB) and two calls on the same remote object may be executed concurrently unless you implement some synchronization. Your approach is correct, and synchronizing all methods will indeed ensure that no two of them run at the same time on the same object. Note: The keyword synchronized alone is equivalent to synchronized( this ).


2)我有一个服务器
定期执行的方法。它用于
做清理。我必须确保
当远程客户端运行/使用任何RMI方法
时,这个特定方法不执行

2) I have a method that the server executes periodically. It is used to do cleanups. I have to make sure that this particular method does not execute when there is any RMI method being run/used by remote clients.

如果清理作业在另一个类中,则需要定义一个锁,您将在远程对象和清理作业之间共享。在远程对象中,定义将用作锁的实例变量。

If the cleanup job is in another class, you will need to define a lock that you will share between the remote object and the cleanup job. In the remote object, define an instance variable that you will use as a lock.

protected Object lock = new Object();

按照惯例,人们使用对象代替这个目的。然后你需要使用 synchronized(remoteObj.lock){...} 获取定期作业中的锁,假设它在同一个包中。

By convention, people use an Object for this purpose. Then you need to grab the lock in your periodic job with synchronized( remoteObj.lock ) { ... }, assuming it's in the same package.

远程对象中的其他方法需要以相同的方式同步(单独 synchronized 是不够的),以便远程方法调用和定期工作都是独家的。

The other methods in the remote object will need to be synchronized the same way (synchronized alone is not enough), so that remote method calls and periodic job are both exclusive.


我已经考虑将RMI
方法实现为静态并包括
清理方法在RMI
界面内部,但它似乎不是
解决问题的优雅方式。

I have considered implementing RMI methods as static and including that cleanup method inside the RMI interface, but it does not seem to be an elegant way of solving the problem.

我还写了清理方法
同步时,RMI界面内的
。当我以
测试运行它时,方法之间似乎没有
冲突,但是我不能确定b $ b。

I have also written the cleanup method inside the RMI interface as synchronized. When I ran it for testing, there did not seem to be collisions between methods, but I cannot be sure.

如果我理解得很好,你想让清理逻辑成为静态方法吗?单独使用 synchronized 的静态方法会抓取类的锁定。使用 synchronized 的常规方法会抓取对象实例的锁定。这些隐式锁不一样!

If I understand well, you would like to have the cleanup logic be a static method? A static method with synchronized alone grabs a lock on the class. A "regular" method with synchronized grabs a lock on the object instance. These are not the same implicit locks!

但如果只实例化了一个远程对象,则可以进行锁定 static(这与锁定类相同,但更清晰一点)。清理代码也可以是静态的,并且与远程对象在同一个类中。

But if you have only one remote object instantiated, you can make the lock static (That's the same as locking on the class, but is a bit cleaner). The cleanup code can then be static as well and be in the same class as the remote object or not.

Skeleton:

public class MyRemoteClass {
   public static Object lock = new Object();

   public void doStuff()
   {
       synchronized( lock ) { ... }
   }
}

public class Cleanup {
   public static void doIt()
   {
       synchronized( MyRemoteClass.lock ) { ... }
   }
}

这篇关于Java RMI和线程同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆