你能不能告诉我是否应该在等待条件之前释放锁?
try { lock.lock(); while (isNotEmpty()) { condition.await(); } } finally { lock.unlock(); }
要么
try { lock.lock(); while (isNotEmpty()) { lock.unlock(); condition.await(); } } finally { lock.unlock(); }@H_502_8@
解决方法
不,您不需要显式释放锁,等待将自动释放.从
javadoc:
The lock associated with this Condition is atomically released and the
current thread becomes disabled for thread scheduling purposes[…]
和:
@H_502_8@ @H_502_8@ 原文链接:https://www.f2er.com/java/126371.htmlThe current thread is assumed to hold the lock associated with this
Condition
when this method is called.