LinkedBlockingQueue源码解析

2021-10-28  本文已影响0人  墨_0b54
static class Node<E> {
    E item;
    Node<E> next;
    Node(E x) { item = x; }
}
private final int capacity; //最大容量限制
private final AtomicInteger count = new AtomicInteger(); //当前占用容量
transient Node<E> head; //头指针
private transient Node<E> last; //尾指针
private final ReentrantLock takeLock = new ReentrantLock(); //take的可重入锁
private final Condition notEmpty = takeLock.newCondition(); //队列为空时的线程等待池
private final ReentrantLock putLock = new ReentrantLock(); //put的可重入锁
private final Condition notFull = putLock.newCondition(); //队列满时的队列等待池
public LinkedBlockingQueue(int capacity) { //初始化最大容量
    if (capacity <= 0) throw new IllegalArgumentException();
    this.capacity = capacity;
    last = head = new Node<E>(null); //初始化头尾
}
public void put(E e) throws InterruptedException {
    if (e == null) throw new NullPointerException();
    int c = -1;
    Node<E> node = new Node<E>(e);
    final ReentrantLock putLock = this.putLock;
    final AtomicInteger count = this.count;
    putLock.lockInterruptibly();
    try {
        while (count.get() == capacity) { //队列满了阻塞
            notFull.await();
        }
        enqueue(node);
        c = count.getAndIncrement();
        if (c + 1 < capacity)
            notFull.signal(); //唤醒put线程
    } finally {
        putLock.unlock();
    }
    if (c == 0)
        signalNotEmpty(); //唤醒take线程
}
上一篇 下一篇

猜你喜欢

热点阅读