I understand various bits of the code below are missing, my question is with regards to the synchronization mechanism in RemoteImplemntation. I also understand there are several questions on this site and others regarding RMI and synchronization; here I am looking for clear confirmation/contradiction.
My question is this: Is this a reasonable way of synchronizing calls to the add method when I have several clients running at the same time? I.e., Assuming all of the ids are different, will the treeset be of size 20,000 after 20 clients have finished executing on different machines, which were started at the same time?
public interface RemoteInterface extends Remote {
void add(String id) throws RemoteException;
}
public class RemoteImplemenation implements RemoteInterface{
TreeSet<String> ids = new TreeSet<String>();
final Object lock = new Object();
public void add(String id) {
synchronized(lock) {
ids.add(id);
}
}
}
public class Client {
public static void main(String[] args) {
RemoteInterface remote = (RemoteInterface)Naming.lookup(...);
for (int i=0;i<1000;i++) {
remote.add(ipaddress+"_"+i);
}
}
}