別のスレッドの notify() が、あるスレッドの wait() の前に呼び出される可能性はありませんか? それは私に起こっています。
クライアントはターゲットから値を要求し、結果変数 RV を待ちます。ターゲットがクライアント自体の場合、正しい結果で RV を更新し、別のスレッドで RV の notify() を呼び出します。
class EMU {
ResultVar RV;
Address my_address;
ResultVar findValue(String key) {
String tgt = findTarget(key);
sendRequest(tgt, key);
synchronized(RV) {
RV.wait();
}
return RV;
}
Runnable Server = new Runnable() {
public void run() {
//code to receive connections. Assume object of type Request is read from the stream.
Request r = (Request) ois.readObject();
if(r.requesterAddr.compareTo(my_address) == 0) {
String val = findVal(key);
RV.putVal(val);
synchronized(RV){
RV.notify();
}
}
}
};
}
問題は、リクエスターがすべての「ネットワーキング」 (上記の例では sendReqest) を完了する前に、結果変数で結果が更新されることです。リクエスタ スレッドが wait() を呼び出すと、notify が既に呼び出されているため、プログラムは続行されません。
どうすればそれを防ぐことができますか?