views:

225

answers:

2

Hello,

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

+1  A: 

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.

Finbarr
A: 

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.

akf
If they get it after the update ...
EJP
I didn't say anything about a race condition.
EJP