wait() の呼び出しを報告します。
wait() は通常、ある条件が true になるまでスレッドを一時停止するために使用されます。
このようなスレッドは別の理由で呼び起こされている可能性があるため、wait() を呼び出した後は条件を確認する必要があります。
これを実現するには、ループを使用するのが簡単です。
例:
class BoundedCounter {
private int count;
synchronized void inc() throws InterruptedException {
if (count >= 10) wait();
++count;
}
}
次のようなコードが適切です:
class BoundedCounter {
private int count;
synchronized void inc() throws InterruptedException {
while (count >= 10) wait();
++count;
}
}