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;
}
}