Java – condition – should be unlocked before waiting?
•
Java
Can you tell me if I should release the lock before waiting for the condition?
try {
lock.lock();
while (isNotEmpty()) {
condition.await();
}
} finally {
lock.unlock();
}
or
try {
lock.lock();
while (isNotEmpty()) {
lock.unlock();
condition.await();
}
} finally {
lock.unlock();
}
Solution
No, you do not need to explicitly release the lock, waiting will automatically release From Javadoc:
And:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
