LinkedBlockingQueue

    /**
     在队列尾插入指定元素
     **/
    public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        int c = -1;
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        putLock.lockInterruptibly();
        try {
            try {
                //当队列已满,插入队列的线程等待
                while (count.get() == capacity)
                    notFull.await();
            } catch (InterruptedException ie) {
                notFull.signal();
                throw ie;
            }
            insert(e);
            c = count.getAndIncrement();
            //队列未满,释放插入队列的等待线程
            if (c + 1 < capacity)
                notFull.signal();
        } finally {
            putLock.unlock();
        }
        //队列为空时,可能有取数据的线程等,此时已有数据加入队列,释放取数据的线程
        if (c == 0)
            signalNotEmpty();
    }

 

你可能感兴趣的:(LinkedBlockingQueue)