Java源码阅读(四)—— ArrayBlockingQueue

介绍

依赖关系

依赖关系

源码

构造方法


public ArrayBlockingQueue(int capacity) {
        this(capacity, false);//默认构造非公平的有界队列
    }

public ArrayBlockingQueue(int capacity,//指定队列大小
                               boolean fair,//指定是否使用公平锁
                              Collection<? extends E> c//指定初始化时加入集合
                               ) {
        this(capacity, fair);
        //初始化ReentrantLock重入锁
        final ReentrantLock lock = this.lock;
        lock.lock(); // Lock only for visibility, not mutual exclusion
        try {
            int i = 0;
            try {
                for (E e : c) {
                    checkNotNull(e);
                    items[i++] = e;
                }
            } catch (ArrayIndexOutOfBoundsException ex) {
                throw new IllegalArgumentException();
            }
            count = i;
            putIndex = (i == capacity) ? 0 : i;
        } finally {
            lock.unlock();
        }
    }


参考文档

10.并发包阻塞队列之ArrayBlockingQueue

posted @ 2019-05-15 09:38  清泉白石  阅读(135)  评论(0编辑  收藏  举报