Java RMI方法同步

发布于 2024-08-28 07:41:36 字数 238 浏览 7 评论 0原文

我有一个存储在“服务器”上的类,多个客户端可以调用此方法。该方法返回一个类。现在,当客户端调用此类中的访问器方法时,例如设置访问器方法。我希望服务器上的对象能够在所有其他客户端之间更新和同步。

我如何

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-09-04 07:41:36

synchronized 关键字不执行此功能,而是 synchronized 关键字尝试获取当前对象的内在锁。

让所有客户端将自身导出为 Remote 对象,并向服务器注册更新。当您的客户端之一更新服务器时,服务器将调用所有已注册的远程客户端,要求它们刷新。

这可以通过观察者模式来实现。如果您的所有客户端都实现了 java.util.Observer 接口,并且服务器或(如果您的类是 Remote)您关心更新的类可以扩展java.util.Observable。当类/服务器更新时,可以通知观察者新值,这将节省客户端向服务器请求新值的一些网络延迟。

The synchronized keyword does not perform this function, rather, the synchronized 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 registered Remote clients asking them to refresh.

This could be achieved with the Observer pattern. If all of your clients implement the java.util.Observer interface, and either the server or (if your class is a Remote) the class you are concerned with updating can extend java.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.

甜心 2024-09-04 07:41:36

我正在尝试理解这个问题。我有两个想法:

  1. 您正在调用 getObject() 远程调用,然后您想要更新返回的对象并使其在所有客户端之间同步。这行不通。将对象传递到客户端后,更新仅在该客户端本地进行。

  2. 您询问是否可以同步远程调用,并让该值在更新后可供所有客户端使用。这里的答案是。假设您的所有客户端都不是在本地而是通过远程调用访问该值:

     public String getStatus() 抛出 RemoteException;
    

如果是这种情况,那么您的客户端调用,无论它们是否调用了 set 方法,都将全部收到新值。

I am trying to understand the question. I have two ideas in mind:

  1. 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.

  2. 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:

      public String getStatus() throws RemoteException;
    

If that is the case, then your client calls, regardless of whether they called the set method, will all receive the new value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文