Java RMI和同步方法 [英] Java RMI and synchronized methods

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

问题描述

我正在研究分布式系统一书(由Tanenbaum& Van Steen撰写),他们说的东西似乎与许多人在Java RMI和同步方法上的想法相冲突。

I'm studying the book "Distributed Systems" (by Tanenbaum & Van Steen) and they say something that seems to conflict to what seems to be instead thought by many on Java RMI and synchronized methods.

我认为在远程对象实现上使用 synchronized方法(因此在服务器上运行的实际实现),即使在对该方法的调用来自不同的客户端机器(通过代理调用方法...也称为存根)。

What I thought is that using a synchronized method on a Remote Object implementation (so the real implementation running at the server) concurrent execution of that method is prevented even when the calls to that method are from different clients machines (calling the method via a Proxy... aka a Stub).

我看过很多人都有同样的观点,请看这里例如: Java RMI和线程同步问题

I've seen that a lot of people have the same opinion, look here for example: Java RMI and Thread Synchronization questions

在本书中,它表示使用RMI时不会阻止同步方法的并发执行。

In the book it's instead said that concurrent execution of synchronized methods is not prevented when using RMI.

这是书中的相关摘录(你可以重新仅使用粗体句子,但如果您愿意,可以阅读上下文:

Here's the relevant excerpt from the book (you can read the bold sentence only, but you can read the context if you prefer to):


逻辑上,阻止远程对象
很简单。假设客户端A调用
远程
对象的同步方法。要访问远程
对象看起来总是与本地对象完全相同
,在实现$ b的
客户端存根中阻止A所需的
$ b对象的接口,A有
直接访问。同样,另一台机器上的另一个
客户端需要在本地阻止
以及
,然后才能将其请求发送到
服务器。结果是我们
需要在不同的机器上同步不同的客户
。我们在Chap中讨论了
。 6,分布式
同步可能相当复杂。

Logically, blocking in a remote object is simple. Suppose that client A calls a synchronized method of a remote object. To make access to remote objects look always exactly the same as to local objects, it would be necessary to block A in the client-side stub that implements the object's interface and to which A has direct access. Likewise, another client on a different machine would need to be blocked locally as well before its request can be sent to the server. The consequence is that we need to synchronize different clients at different machines. As we discussed in Chap. 6, distributed synchronization can be fairly complex.

另一种方法是
允许仅在服务器上阻塞。在
原则中,这工作正常,但是当客户端在服务器处理
的调用时崩溃
时会出现
问题。正如我们在
中所讨论的那样。 8,我们可能需要相对
复杂的协议来处理这个
的情况,而这可能
显着影响远程方法
调用的整体
性能。

An alternative approach would be to allow blocking only at the server. In principle, this works fine, but problems arise when a client crashes while its invocation is being handled by the server. As we discussed in Chap. 8, we may require relatively sophisticated protocols to handle this situation, and which that may significantly affect the overall performance of remote method invocations.

因此,Java RMI
的设计者已经选择将
远程对象的阻塞限制为仅代理
(Wollrath等人) 。,1996)。这意味着
同一进程中的线程将同时阻止
访问同一个远程对象
,但不同进程中的
线程将不会
显然,这些同步
语义是棘手的:在语法
级别(即,在阅读源代码时)
我们可能会看到一个漂亮,干净的设计。当分布式应用程序实际执行
时,只有
,如果
已在设计时处理,则可能会出现意外的
行为。
[...]

Therefore, the designers of Java RMI have chosen to restrict blocking on remote objects only to the proxies (Wollrath et al., 1996). This means that threads in the same process will be prevented from concurrently accessing the same remote object, but threads in different processes will not. Obviously, these synchronization semantics are tricky: at the syntactic level (ie, when reading source code) we may see a nice, clean design. Only when the distributed application is actually executed, unanticipated behavior may be observed that should have been dealt with at design time. [...]

我认为文章Java系统的分布式对象模型(此处可用)在文本中以注释<$ c引用$ c> Wollrath等,1996 括号之间。然而,我在该论文中找到的唯一相关段落是这一段:

I think that the paper "A Distributed Object Model for the Java System" (available here) is referenced in the text by the note Wollrath et all, 1996 between parenthesis. However the only relevant paragraph I've found on that paper is this one:


由于本地和远程$ b的故障模式不同$ b对象,分布式等待和通知需要在涉及
的实体之间使用
更复杂的协议(例如,客户端崩溃不会导致
永远锁定远程对象) ,以及
这样,不能轻易地适应Java中的本地线程
模型。因此,客户端可以在远程引用上使用notify和wait
方法,但该客户端必须是
才能知道此类操作不涉及实际的远程
对象,只涉及本地代理(stub) )对于远程
对象。

Due to the differing failure modes of local and remote objects, distributed wait and notification requires a more sophisticated protocol between the entities involved (so that, for example, a client crash does not cause a remote object to be locked forever), and as such, cannot be easily fitted into the local threading model in Java. Hence, a client can use notify and wait methods on a remote reference, but that client must be aware that such actions will not involve the actual remote object, only the local proxy (stub) for the remote object.

我是以错误的方式解释文本还是事实上声明了同步方法使用RMI时不是那么同步?

Am I interpreting the text in the wrong way or is in fact stated that synchronized methods are "not so synchronized" when using RMI?

推荐答案

您的第一个参考资料是在单个VM实例中调用在RMI Stub(客户端到RMI服务器)上将在内部同步。也就是说,存根(或代理,如文本似乎称之为)本身将阻止多个线程同时调用远程服务器上的方法。但是,它澄清了两个具有远程服务器存根的虚拟机不会被阻止同时调用远程服务器(这很明显,因为它们不能共享锁,而且RMI本身不会阻止服务器的并发)。如果这是不合需要的,RMI服务器必须实现一个锁定机制来防止多个并发调用。

What your first reference is saying is that within a single VM instance, invocations on an RMI Stub (client to an RMI server) will be internally synchronized. That is, the stub (or proxy, as the text seems to call it) itself will prevent multiple threads from concurrently invoking a method on the remote server. It clarifies, however, that two VMs each with stubs for a remote server will not be blocked from concurrently invoking the remote server (which is obvious, because they cannot share a lock, and RMI itself does not prevent concurrency at the server). If this is undesirable, the RMI server will have to implement a locking mechanism to prevent multiple concurrent invocations.

第二个引用不会与第一个引用相矛盾。第二个只是说明如果你尝试在存根上进行同步,它只会在本地锁定,并且不会影响远程服务器的并发性。

The second reference does not in anyway contradict the first. The second merely clarifies that if you try to synchronize on a stub, it will only be locked locally, and will not impact the concurrency of the remote server.

将两者结合起来我们可以看到,在存根上同步会阻止同一个VM中的多个线程同时访问远程,但不会阻止不同虚拟机中的线程同时访问。

Combining the two texts, we can read that synchronizing on a stub will prevent the multiple threads in the same VM from concurrently accessing the remote, but will not prevent threads in distinct VMs from concurrent access.

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

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