java.util.concurrent.locks.Condition.await()
。
await()
和相关方法通常用于挂起线程,直到某个条件变为 true 为止。
由于线程可能由于不同原因而被唤醒,因此应在 await()
调用返回后检查条件。
使用循环很容易做到这一点。
示例:
void acquire(Condition released) throws InterruptedException {
released.await();
}
优良的代码应类似于:
void acquire(Condition released) throws InterruptedException {
while (acquired) {
released.await();
}
}