Java:在ArrayBlockingQueue中调用notFull.signal()put方法是一个bug?

时间:2014-08-05 09:21:46

标签: java

ArrayBlockingQueue内,在put method内,我不明白为什么在抓取notFull.signal()之后会调用InterruptedException。如果thread1thread2一起等待notFull,则线程3中断线程1.线程1捕获interruptedException并发出信号thread2,thread2将元素放入物品,但物品仍然满了,这是一个错误吗?

把方法代码:

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();
    }
}

1 个答案:

答案 0 :(得分:0)

你是否错过了那里不断检查大小的while循环?它只需再次拨打await()即可。一个元素不会被添加,直到它突然出现在while循环中。