Search code examples
javasynchronizationrmi

Java RMI method synchronization


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


Solution

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