I have a method on an RMI server, and this method changes a state variable.
I don't want multiple RMI clients to modify this variable concurrently (i.e.
by calling the method on their stub).
Should I make the method synchronized? Is this class thread-safe?
The class is shown below.
public class RMIServer extends UnicastRemoteObject
implements ServerInterface {
private int state = 0;
RMIServer() throws Exception {
super();
String rmiObjectName =
"rmi://"+ InetAddress.getLocalHost() + "/RMIServer";
Naming.rebind(rmiObjectName, this);
}
public void update_state(int increment){
state = state + increment;
}
}
Tom Hawtin - 07 May 2007 17:59 GMT
> I have a method on an RMI server, and this method changes a state variable.
>
> I don't want multiple RMI clients to modify this variable concurrently (i.e.
> by calling the method on their stub).
>
> Should I make the method synchronized? Is this class thread-safe?
You could make it synchronized, but in this case AtomicInteger would be
suffice.
Tom Hawtin