gpt4 book ai didi

java - 我们需要 ArrayBlockingQueue.put(E) 中的 Condition.signal

转载 作者:行者123 更新时间:2023-11-30 04:28:25 25 4
gpt4 key购买 nike

我正在查看ArrayBlockingQueue.put(E)的代码。我们真的需要在 catch block 中调用 notFull.signal 吗?该信号不应该由消费者调用吗?

public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
final E[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (count == items.length)
notFull.await();
} catch (InterruptedException ie) {
notFull.signal(); // propagate to non-interrupted thread
throw ie;
}
insert(e);
} finally {
lock.unlock();
}
}

最佳答案

问题似乎是,如果发出了一个条件信号,并且线程被中断,则 await() 会消耗该信号并抛出 InterruptedException。但是,javadoc 特别禁止 - 如果 await() 抛出 InterruptedException,则它不得消耗信号。

类似的问题也适用于Object.wait()。在 1.4 中,javadoc 不是很明确。从 1.5 开始,javadoc 说

Throws: InterruptedException - if another thread interrupted the current thread before or while the current thread was waiting for a notification.

这似乎暗示如果 wait() 抛出 InterruptedException,它一定不能使用通知。

关于java - 我们需要 ArrayBlockingQueue.put(E) 中的 Condition.signal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15277458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com