Java – condition – should be unlocked before waiting?

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
分享
二维码
< <上一篇
下一篇>>