0
スレッドの待機
以下コードで、SampleThreadが別モジュールを実行して、その実行終了結果を受けてEventListenerがtransfer関数を呼ぶような実装があったとして、
仮にSampleThread瞬時に終わり、runメソッドのwait関数を実行する前に、
notifyAll()が実行されることがありますでしょうか?
synchronizedしているから問題ないかが心配なところです。
通常は、notifyAllが先に実行された後に、waitが実行されて、無期限に待機状態になってしまうことを防ぐために、wait実行前に、実行条件を設ける実装をするかと思いますが、その実装は必要でしょうか?
public class Sample extends Thread implements EventListener {
public Sample() {
this.start();
}
/**
* メイン処理メソッド
*/
public void run() {
synchronized (this) {
try {
SampleThread thread = new SampleThread();
thread.start();
this.wait();
} catch (InterruptedException e) {
}
}
}
/**
* 結果を受け取るためのリスナです。
*/
public void transfer() {
synchronized (this) {
this.notifyAll();
}
}
}