ArrayBlockingQueue 和LinkedBlockingQueue 代码解析(JDK8)

在使用线程池的时候,需要指定BlockingQueue 常用的一般有ArrayBlockingQueue和LinkedBlockingQueue
有一天被问到有什么区别没回答上来,因此从代码的层面解析一下
1 ArrayBlockingQueue
顾名思义,就是用Array来实现的queue Blockqing 则说明是线程安全的

public class ArrayBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable {
    private static final long serialVersionUID = -817911632652898426L;
    final Object[] items;
    int takeIndex;
    int putIndex;
    int count;
    final ReentrantLock lock;
    private final Condition notEmpty;
    private final Condition notFull;
}

items 存储数据的数组
takeIndex 取数据时数组的下标
putIndex 放数据时的下标
count 数据的数量
lock 使用ReentrantLock 来保证线程安全
notEmpty 非空信号量,用来进行取数据时的信号量
notFull 非满信号量,在写数据时数据满时的等待信号量
1 构造函数

    public ArrayBlockingQueue(int capacity) {
        this(capacity, false);
    }
    public ArrayBlockingQueue(int capacity, boolean fair) {
        if (capacity <= 0)
            throw new IllegalArgumentException();
        this.items = new Object[capacity];//指定数组大小
        lock = new ReentrantLock(fair); //根据参数确定lock是否为公平锁,默认为false
        notEmpty = lock.newCondition(); //新建两个lock的信号量
        notFull =  lock.newCondition();
    }

2 写数据
研究代码发现 put add offer三个方法都调用了enqueue方法,ArrayBlockingQueue 将对数组的实际操作在jdk8抽象了出来,相对于jdk7进行了一定优化

    /**
     * Inserts element at current put position, advances, and signals.
     * Call only when holding lock.
     */
    //该方法只有在对象获取到锁之后才能调用
    private void enqueue(E x) {
        // assert lock.getHoldCount() == 1;
        // assert items[putIndex] == null;
        final Object[] items = this.items; //获取数组对象
        items[putIndex] = x; //putIndex 默认值为0
        if (++putIndex == items.length) //在putIndex到达数组尾部时,重新指向数组第一个位置
            putIndex = 0;
        count++; //数组元素+1
        notEmpty.signal(); //非空信号发送
    }

(1) offer
offer方法 尝试插入数据,在数组满时返回false,正常插入 返回true

    public boolean offer(E e) {
        checkNotNull(e); //校验数据是否为null
        final ReentrantLock lock = this.lock; //获取对象锁
        lock.lock(); //对当前对象加锁
        try {
            if (count == items.length) //如果数组满,返回false
                return false;
            else {
                enqueue(e); //数组没满,插入数据,返回true
                return true;
            }
        } finally {
            lock.unlock(); //释放锁
        }
    }

(2) add
ArrayBlockingQueue 调用了父类AbstractQueue的add方法,
在插入成功时返回true,在插入失败(数组满)时,抛出异常
AbstractQueue 的add方法调用了offer()方法,所以add是offer的功能升级版

    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

(3) put
put方法 在进行数据插入时,会尝试获取锁并相应异常,同时,在数组满时,会一致等待,直到数组有了空闲空间

    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(); //在数据正常插入或者其他线程抛出异常后,解锁
        }
    }

(4) offer(E e, long timeout, TimeUnit unit)
ArrayBlockingQueue 还提供了一种超时配置的方法,在数组数据满超过timeout后返回fasle

  public boolean offer(E e, long timeout, TimeUnit unit)
        throws InterruptedException {

        checkNotNull(e);
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == items.length) {
                if (nanos <= 0)
                    return false;
                nanos = notFull.awaitNanos(nanos); //condition超时后 返回-1
            }
            enqueue(e);
            return true;
        } finally {
            lock.unlock();
        }
    }

3 取数据
和写数据一样,取数据jdk8也进行了一定优化 统一调用dequeue方法

   private E dequeue() {
        // assert lock.getHoldCount() == 1;
        // assert items[takeIndex] != null;
        final Object[] items = this.items; 
        @SuppressWarnings("unchecked")
        E x = (E) items[takeIndex]; //获取最老数据
        items[takeIndex] = null; //最老数据位置置空
        if (++takeIndex == items.length) //下标到达最后 置零
            takeIndex = 0;
        count--; 
        if (itrs != null) //itrl目前没看到初始化的位置 ,暂时不清楚有什么用
            itrs.elementDequeued();
        notFull.signal();
        return x;
    }

(1) poll(E e, long timeout, TimeUnit unit)
很简单 列表为空返回null否则放回对应数据

    public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock(); //加锁
        try {
            return (count == 0) ? null : dequeue(); //列表为空返回null否则放回对应数据
        } finally {
            lock.unlock(); //解锁
        }
    }

(2) take(E e, long timeout, TimeUnit unit)
尝试加锁,在数组为空时一直等待,直到有新数据或者被外部中断

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

(3) peek(E e, long timeout, TimeUnit unit)
返回最老数据,但是不弹出数据,仅获取数据。在数组为空时返回null
因此一条数据可以重复peek多次

    public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return itemAt(takeIndex); // null when queue is empty
        } finally {
            lock.unlock();
        }
    }
    final E itemAt(int i) {
        return (E) items[i];
    }

(4) poll(long timeout, TimeUnit unit)(E e, long timeout, TimeUnit unit)
也提供了等待超过timeout 返回null的poll方法

    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
        long nanos = unit.toNanos(timeout);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == 0) {
                if (nanos <= 0)
                    return null;
                nanos = notEmpty.awaitNanos(nanos); //如果超时awaitNanos 返回-1 ,最后返回null
            }
            return dequeue();
        } finally {
            lock.unlock();
        }
    }

2 LinkedBlockingQueue
顾名思义,就是使用链表来存储的线程安全的队列

public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable {
    private static final long serialVersionUID = -6903933977591709194L;
    private final int capacity;
    private final AtomicInteger count;
    transient LinkedBlockingQueue.Node<E> head;
    private transient LinkedBlockingQueue.Node<E> last;
    private final ReentrantLock takeLock;
    private final Condition notEmpty;
    private final ReentrantLock putLock;
    private final Condition notFull;
}

capacity 链表的最大长度,默认为Integer.MAX_VALUE
count 元素数量
head 头节点
last 尾节点
takeLock 取数据锁
notEmpty 非空信号量
putLock 写数据锁
notFull 非满信号量
LinkedBlockingQueue 采用了读写锁分离,因此在短时间内产生大量读写操作时,
比arrayBlockingQueue性能更加优秀
1 构造函数

    public LinkedBlockingQueue() {
        this(Integer.MAX_VALUE);
    }
    public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity; //设置最大长度
        last = head = new Node<E>(null); //
    }

2写数据
LinkedBlockingQueue同样提供了三个函数 put offer add
同样提供了enqueue方法,该方法仅在获取到putLock 后执行

    private void enqueue(Node<E> node) {
        // assert putLock.isHeldByCurrentThread();
        // assert last.next == null;
        last = last.next = node; //在尾节点添加数据
    }

(1) offer(E e)

    public boolean offer(E e) {
        if (e == null) throw new NullPointerException();
        final AtomicInteger count = this.count; //获取元素数量
        if (count.get() == capacity) //如果链表长度到达上限,返回null
            return false;
        int c = -1;
        Node<E> node = new Node<E>(e); //创建新节点
        final ReentrantLock putLock = this.putLock; 
        putLock.lock();  //写锁加锁
        try {
            if (count.get() < capacity) { //如果没有到达链表上限
                enqueue(node);   //新增节点
                c = count.getAndIncrement(); //获取元素数量并将count+1(c=count,count++),
                                             //读写锁分离,链表数量可能有减少
                if (c + 1 < capacity)  //如果链表数量没有达到上限,非满信号量通知
                    notFull.signal();
            }
        } finally {
            putLock.unlock(); //解锁
        }
        if (c == 0)  //如果链表原来的数量为0
            signalNotEmpty(); //非空信号量通知
        return c >= 0;  //返回插入结果 成功返回true,失败返回fasle
    }
    private void signalNotEmpty() {
        final ReentrantLock takeLock = this.takeLock; //获取读锁
        takeLock.lock(); //读锁加锁,防止数据被读取
        try {
            notEmpty.signal(); //非空信号量通知
        } finally {
            takeLock.unlock(); //读锁解锁
        }
    }

(2) add(E e)
和ArrayBlockingQueue一样,直接调用offer方法,在新增成功后返回true,在新增失败后直接抛出异常
(3) put(E e)
put操作 和offer操作基本一致,只不过在链表满时进行等待,知道链表节点减少

 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();
        } finally {
            putLock.unlock(); //释放写锁
        }
        if (c == 0) 
            signalNotEmpty();
    }

(4) offer(E e, long timeout, TimeUnit unit)
和ArrayBlockingQueue一样,如果链表ch长度到达上限,就等待timeout ,超时后直接返回fasle
3 读取数据
上dequeue

    private E dequeue() {
        // assert takeLock.isHeldByCurrentThread();
        // assert head.item == null;
        Node<E> h = head; // 获取头节点
        Node<E> first = h.next; //first设置为新的头节点
        h.next = h; // help GC //需要移除的节点next指向自己帮助gc
        head = first;  //head 置为新的头节点
        E x = first.item; //获取返回值得item
        first.item = null; //first item设置为null 
        return x; //返回item
    }

(1) poll(E e, long timeout, TimeUnit unit)

    public E poll() {
        final AtomicInteger count = this.count; //获取数量
        if (count.get() == 0) //如果链表节点数量为空 返回null
            return null;
        E x = null;
        int c = -1;
        final ReentrantLock takeLock = this.takeLock; //获取读锁并加锁
        takeLock.lock();
        try {
            if (count.get() > 0) {
                x = dequeue(); //获取数据
                c = count.getAndDecrement(); //数量-1
                if (c > 1)  //剩余节点>1
                    notEmpty.signal(); //非空信号通知
            }
        } finally {
            takeLock.unlock(); //读锁解锁
        }
        if (c == capacity) //可能有线程在等在非满信号,-1前数量=限定长度
            signalNotFull();
        return x;
    }
    private void signalNotFull() {
        final ReentrantLock putLock = this.putLock; //获取写锁并加锁
        putLock.lock();
        try {
            notFull.signal(); //非满信号通知
        } finally {
            putLock.unlock(); //写锁解锁
        } 
    }

(2) peek(E e)
存在返回数据,不存在返回null,链表节点不便,仅获取数据

    public E peek() {
        if (count.get() == 0)
            return null;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            Node<E> first = head.next;
            if (first == null)
                return null;
            else
                return first.item;
        } finally {
            takeLock.unlock();
        }
    }

(3) take(E e)
链表到达最大长度。等待,可以被异常中断

    public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
            while (count.get() == 0) {
                notEmpty.await();
            }
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }

(4) poll(long timeout, TimeUnit unit)
等待超过timeout 返回null

3 两者的区别
1 Linked读写锁分离,在短时间内发生大量读写交替操作时性能高
2 Array在读写操作时不需要维护额外节点,空间较少
3 Array使用int count Linked使用AtomicInteger ,
因此:Array使用唯一Lock来保证count强一致性,Linked使用Atomic来保证count的准确性

上一篇:一文读懂 深度强化学习算法 A3C (Actor-Critic Algorithm)


下一篇:18 Issues in Current Deep Reinforcement Learning from ZhiHu