Java RMI方法同步
我有一个存储在“服务器”上的类,多个客户端可以调用此方法。该方法返回一个类。现在,当客户端调用此类中的访问器方法时,例如设置访问器方法。我希望服务器上的对象能够在所有其他客户端之间更新和同步。
我如何
public synchronized setStatus(String s) { this.status = s; }
在java中使用:来实现这一点。
谢谢
I have a class that is stored on the 'server' and multiple clients can call this method. This method returns a class. Now when clients call accessor methods within this class for example a set accessor method. I want the object on the server to be updated and synchronized across all the other clients.
How do I use:
public synchronized setStatus(String s) { this.status = s; }
within java to achieve this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
synchronized
关键字不执行此功能,而是synchronized
关键字尝试获取当前对象的内在锁。让所有客户端将自身导出为
Remote
对象,并向服务器注册更新。当您的客户端之一更新服务器时,服务器将调用所有已注册的远程
客户端,要求它们刷新。这可以通过观察者模式来实现。如果您的所有客户端都实现了 java.util.Observer 接口,并且服务器或(如果您的类是 Remote)您关心更新的类可以扩展
java.util.Observable
。当类/服务器更新时,可以通知观察者新值,这将节省客户端向服务器请求新值的一些网络延迟。The
synchronized
keyword does not perform this function, rather, thesynchronized
keyword attempts to obtain the intrinsic lock for the current object.Have all of your clients export themselves as
Remote
objects, and register for updates with the server. When one of your clients updates the server, the server will then call all of the registeredRemote
clients asking them to refresh.This could be achieved with the
Observer
pattern. If all of your clients implement thejava.util.Observer
interface, and either the server or (if your class is aRemote
) the class you are concerned with updating can extendjava.util.Observable
. When the class/server is updated, the Observers can be notified with the new value, which will save on some network delay of the clients asking the server for the new value.我正在尝试理解这个问题。我有两个想法:
您正在调用
getObject()
远程调用,然后您想要更新返回的对象并使其在所有客户端之间同步。这行不通。将对象传递到客户端后,更新仅在该客户端本地进行。您询问是否可以同步远程调用,并让该值在更新后可供所有客户端使用。这里的答案是是。假设您的所有客户端都不是在本地而是通过远程调用访问该值:
如果是这种情况,那么您的客户端调用,无论它们是否调用了
set
方法,都将全部收到新值。I am trying to understand the question. I have two ideas in mind:
you are calling a
getObject()
remote call, and then you want to update the returned Object and have it synchronized across all clients. This wont work. Once an object is passed to a client, updates are only local to that client.you are asking whether you could synchronize a remote call, and have the value be available to all clients once updated. The answer here is yes. The assumption is that all of your clients are accessing the value, not locally, but through a remote call:
If that is the case, then your client calls, regardless of whether they called the
set
method, will all receive the new value.