Java 我们是否需要ArrayBlockingQueue.put(E)中的Condition.signal
我正在查看Java 我们是否需要ArrayBlockingQueue.put(E)中的Condition.signal,java,concurrency,Java,Concurrency,我正在查看ArrayBlockingQueue.put(E)的代码。我们真的需要调用catch块中的notFull.signal?消费者不应该调用该信号吗 public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); final E[] items = this.items; final ReentrantLock lock = thi
ArrayBlockingQueue.put(E)
的代码。我们真的需要调用catch块中的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();
}
}
我们根本不需要那个电话。最新版本的JDK(1.7.0_17)甚至没有这个catch块
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
notFull.await();
insert(e);
} finally {
lock.unlock();
}
}
问题似乎是,如果一个条件被通知,线程被中断,
await()
将使用该信号并抛出InterruptedException。但是,javadoc明确禁止-如果await()
抛出InterruptedException,则它不得使用信号
类似的问题也适用于Object.wait()
。在1.4中,javadoc不是很明确。javadoc说,从1.5开始
Throws:InterruptedException-如果当前线程在等待通知之前或期间另一个线程中断了当前线程
这似乎意味着如果
wait()
抛出InterruptedException,它就不能使用通知。您使用的是哪个版本的JDK?我使用的是jdk1.6.0\u 23我同意您的论点,但不确定javadoc是如何暗示的。顺便说一句,你知道为什么代码从1.6简化到1.7吗?不知道。你可以把问题发到