Java LinkedBlockQueue在调用Wait时是否获得锁?
我在Java LinkedBlockQueue在调用Wait时是否获得锁?,java,concurrency,Java,Concurrency,我在LinkedBlockingQueue public E take() throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); t
LinkedBlockingQueue
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
notEmpty.await();
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
wait
方法释放锁,在发出信号后,再次在while循环中,它似乎没有锁。在notEmpty
中,它指定如果在调用wait
期间未持有锁,将抛出IllegalMonitorStateException
这使我困惑。。它最终是否持有锁?当然,它持有锁。为了判断数据是否可以从队列中获得令牌,它使用循环。
当队列仍然为空时,它应该再次等待,直到
s通知它,并且队列计数不为零。只要await()
返回,它就会保持锁。标准行为类似于Object.wait()
。阅读文档,而不是说“似乎”。这是多年前的JDK代码,您声称它已经从根本上被破坏了。