ArrayBlockingQueue源码详解

ArrayBlockingQueue简介

上篇文章我们说了LinkedBlockingQueue,ArrayBlockingQueue相较于LinkedBlockingQueue,就是ArrayBlockingQueue是基于数组的。其实现的功能和LinkedBlockingQueue一样。

ArrayBlockingQueue源码解析

主要属性

//数据数组
final Object[] items;

//头节点下标
int takeIndex;

//尾节点下标
int putIndex;

//元素个数
int count;

//独占锁,入队和出队公用一个lock,说明不能同时出队和入队
final ReentrantLock lock;

//出队等待条件队列
private final Condition notEmpty;
//入队等待条件队列
private final Condition notFull;

初始化

public ArrayBlockingQueue(int capacity) {
    //false默认lock为非公平锁
    this(capacity, false);
}
public ArrayBlockingQueue(int capacity, boolean fair) {
    if (capacity <= 0)
        throw new IllegalArgumentException();
    this.items = new Object[capacity];
    lock = new ReentrantLock(fair);
    notEmpty = lock.newCondition();
    notFull =  lock.newCondition();
}

offer()

public boolean offer(E e) {
    //private static void checkNotNull(Object v) {
    //    if (v == null)
    //        throw new NullPointerException();
    //}
    checkNotNull(e);

    //获取独占锁
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        //如果队列满了,返回false
        if (count == items.length)
            return false;
        else {
            //入队
            enqueue(e);
            return true;
        }
    } finally {
        lock.unlock();
    }
}
//入队
private void enqueue(E x) {
    final Object[] items = this.items;
    
    items[putIndex] = x;
    //如果putIndex超出数组范围了,就置为0
    if (++putIndex == items.length)
        putIndex = 0;
    count++;
    //唤醒等待出队节点
    notEmpty.signal();
}

add()

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

通过代码可以看出,当队列满的时候,add()会抛出异常,而offer()就是返回false而已。

put()

public void put(E e) throws InterruptedException {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        //如果队列满了,阻塞再notFull等待队列中
        while (count == items.length)
            notFull.await();
        enqueue(e);
    } finally {
        lock.unlock();
    }
}

put()和offer()的区别是,put()在队列满的时候,会阻塞。

poll()

public E poll() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        //如果队列为空,返回null。
        return (count == 0) ? null : dequeue();
    } finally {
        lock.unlock();
    }
}
private E dequeue() {
    
    final Object[] items = this.items;
    @SuppressWarnings("unchecked")
    E x = (E) items[takeIndex];
    items[takeIndex] = null;
    //如果takeIndex等于items.length,将takeIndex = 0
    if (++takeIndex == items.length)
        takeIndex = 0;
    count--;
    //更新迭代器
    if (itrs != null)
        itrs.elementDequeued();
    //唤醒等待入队线程
    notFull.signal();
    return x;
}

take()

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        //如果队列为空,阻塞在notEmpty等待队列中
        while (count == 0)
            notEmpty.await();
        return dequeue();
    } finally {
        lock.unlock();
    }
}

take()和poll()的区别就是当队列为空的时候,take()会阻塞。

peek()

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

总结

ArrayBlockingQueue和LinkedBlockingQueue有以下区别:
1、ArrayBlockingQueue基于数组,LinkedBlockingQueue基于链表
2、ArrayBlockingQueue只有一个ReentrantLock,出队和入队是不能同时进行的,而LInkedBlockingQueue有两个ReentrantLock,出队和入队是可以同时进行的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值