以下のコードのさまざまな部分が欠落していることを理解しています。私の質問は、RemoteImplemntationの同期メカニズムに関するものです。また、RMIと同期に関して、このサイトやその他のサイトにいくつかの質問があることも理解しています。ここで私は明確な確認/矛盾を探しています。
私の質問はこれです:複数のクライアントを同時に実行している場合、これはaddメソッドへの呼び出しを同期する合理的な方法ですか?つまり、すべてのIDが異なるとすると、同時に開始された異なるマシンで20のクライアントの実行が終了した後、ツリーセットのサイズは20,000になりますか?
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);
}
}
}