RMI and thread safety

B

Brian

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;
}
}
 
T

Tom Hawtin

Brian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top