LinkedBlockingQueue

105 阅读2分钟

LinkedBlockingQueue

​ 这是基于链表的BlockingQueue堵塞队列。

​ 在队列这种数据结构中,最先插入的元素将是最先被删除的元素;反之最后插入的元素将是最后被删除的元素,因此队列又称为“先进先出”(FIFO—first in first out)的线性表

​ 它是实现了BlockingQueue和序列化,继承自AbstractQueue。 内部实现了一个静态节点类

    static class Node<E> {
        E item;

        /**
         * One of:
         * - the real successor Node
         * - this Node, meaning the successor is head.next
         * - null, meaning there is no successor (this is the last node)
         * -也就是下一个节点
         */
        Node<E> next;

        Node(E x) { item = x; }
    }

LinkedBlockingWueue的一些参数

    /** The capacity bound, or Integer.MAX_VALUE if none */
	//可以指定大小,不指定的话默认就是Intger的最大值
    private final int capacity;
	

    /** Current number of elements */
	//目前元素的数量
    private final AtomicInteger count = new AtomicInteger();

    /**
     * Head of linked list.
     * Invariant: head.item == null
     */
	//头节点
    transient Node<E> head;

    /**
     * Tail of linked list.
     * Invariant: last.next == null
     */
	//尾节点
    private transient Node<E> last;

    /** Lock held by take, poll, etc */
    private final ReentrantLock takeLock = new ReentrantLock();

    /** Wait queue for waiting takes */
    private final Condition notEmpty = takeLock.newCondition();

    /** Lock held by put, offer, etc */
    private final ReentrantLock putLock = new ReentrantLock();

    /** Wait queue for waiting puts */
    private final Condition notFull = putLock.newCondition();

​ 这里最有意思的是实现了两个锁,分别是在使用take、poll和put,offer的时候,堵塞别的线程。

​ 两个状态,分别在到达capacity的时候使用await方法进行堵塞。等待别的put线程,释放signal()信号。

public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        // Note: convention in all put/take/etc is to preset local var
        // holding count negative to indicate failure unless set.
        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        putLock.lockInterruptibly();
        try {
            /*
             * Note that count is used in wait guard even though it is
             * not protected by lock. This works because count can
             * only decrease at this point (all other puts are shut
             * out by lock), and we (or some other waiting put) are
             * signalled if it ever changes from capacity. Similarly
             * for all other uses of count in other wait guards.
             */
            while (count.get() == capacity) {
                notFull.await();
            }
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();
        } finally {
            putLock.unlock();
        }
        if (c == 0)
            signalNotEmpty();
    }

如果对于整个队列的操作,要进行takeLock和putLock上锁:

    public boolean contains(Object o) {
        if (o == null) return false;
        fullyLock();
        try {
            for (Node<E> p = head.next; p != null; p = p.next)
                if (o.equals(p.item))
                    return true;
            return false;
        } finally {
            fullyUnlock();
        }
    }
void fullyUnlock() {
        takeLock.unlock();
        putLock.unlock();
    }