ArrayBlockingQueue 源码分析学习

ArrayBlockingQueue 是一个基于数组的阻塞队列,是一个有界队列,有界也就意味着,它不能够存储无限多数量的对象。所以在创建 ArrayBlockingQueue 时,必须要给它指定一个队列的大小。

底层维护的是Object数组。主要方法有:

  • put:当添加元素时
    • 当队列已满时会进行阻塞notFull.await(); 不满等待
    • 当队列添加元素之后,会释放take时队列为空的阻塞的锁notEmpty.signal();
  • take: 当取出元素时:
    • 当队列为空时会进行阻塞notEmpty.await(); 不为空等待
    • 当队列取出元素时,会释放put时队列已满的阻塞锁notFull.signal();
  • add: 当添加元素时,如果队列已满则会异常
  • poll: 当取出元素时,如果队列为空则返回 null

实现的方式主要是通过 ReentrantLockCondition . 对两者不熟悉的可以阅读以前的文章

ReentrantLock lock unLock 原理分析

Condition await signal 阻塞和唤醒 原理分析

ArrayBlockingQueue 主要思想的代码

// 存储数据
final Object[] items;
// 下一个take,poll的索引值
int takeIndex;
// 下一个put、offer、add 的索引值
int putIndex;
// 存储当前的数量
int count;
/** Main lock guarding all access */
final ReentrantLock lock;
/** Condition for waiting takes */
private final Condition notEmpty;
/** Condition for waiting puts */
private final Condition notFull;

// 非公平重入锁
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);
    notEmpty = lock.newCondition();
    notFull =  lock.newCondition();
}
// put 添加一个元素到队列中,如果当前队列已满,则阻塞等待
public void put(E e) throws InterruptedException {
    //如果为null,则报空指针异常
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        // 如果当前队列已满,则进行等待
        while (count == items.length)
            notFull.await();
        enqueue(e);
    } finally {
        lock.unlock();
    }
}
// 添加元素到队列中
private void enqueue(E x) {
    // assert lock.getHoldCount() == 1;
    // assert items[putIndex] == null;
    final Object[] items = this.items;
    items[putIndex] = x;
    if (++putIndex == items.length)
        putIndex = 0;
    count++;
    // 释放锁: take()时 队列为空,会进行阻塞
    notEmpty.signal();
}
// 获取一个元素,如果该队列为空,则进行阻塞
public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        // 如果队列为空,则进行阻塞
        while (count == 0)
            notEmpty.await();
        return dequeue();
    } finally {
        lock.unlock();
    }
}
// 取出队列
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)
        itrs.elementDequeued();
    // 释放锁:当put()时,如果队列已满,会进行阻塞
    notFull.signal();
    return x;
}

// 当添加一个元素时,如果队列已满会异常,未满则返回true
public boolean add(E e) {
    return super.add(e);
}
// 如果队列已满,返回false ,天极爱成功返回true
public boolean offer(E e) {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        if (count == items.length)
            return false;
        else {
            enqueue(e);
            return true;
        }
    } finally {
        lock.unlock();
    }
}
// 获取元素,如果队列为空,则返回 null
public E poll() {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        // 如果不为空,则取出首位数据
        return (count == 0) ? null : dequeue();
    } finally {
        lock.unlock();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值