Queue总结

ArrayBlockingQueue

public void put(E e) throws InterruptedException {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly(); // 锁
    try {
        while (count == items.length)
            notFull.await(); // 阻塞
        enqueue(e);
    } finally {
        lock.unlock();
    }
}

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly(); // 锁
    try {
        while (count == 0)
            notEmpty.await(); // 阻塞
        return dequeue();
    } finally {
        lock.unlock();
    }
}

LinkedBlockingQueue

评论

暂无评论