十三、ArrayBlockingQueue

一、概念

  • 基于ReentrantLock实现的阻塞队列,读写都共用同一把锁
  • 底层是一个数组,也就是说,初始化的时候必须要给一个数组长度(不是一个无界队列)
  • 先进先出
  • 默认是一个非公平,可设置为公平
  • Condition的阻塞和唤醒:
    • 阻塞其实就是放在Condition的自己的单项链表里面,当Thread封装成node。
    • 唤醒其实就是将Condition的链表node拿出来,然后放在AQS的阻塞队列里面,然后等待被唤醒

二、使用

写操作

  • add(E e)
    • 队列中添加数据,如果队列满了, 会抛出异常new IllegalStateException(“Queue full”)
      底层调用offer方法
    • 返回值boolean
  • offer(E e)
    • 队列中添加数据,如果队列满了,不会抛出异常,会返回false
    • 返回值boolean
  • offer(E e, long timeout, TimeUnit unit)
    • 队列中添加数据,如果队列满了,会阻塞当前线程timeout时间
    • 返回值boolean
  • put(E e)
    • 队列中添加数据,如果队列满了,会阻塞当前线程,直到有数据或者被打断
    • 无返回值

读操作

  • remove()
    • 获取队列中数据,如果队列中没有数据,则抛出异常throw new NoSuchElementException()
      底层调用poll方法
    • 返回值E
  • poll()
    • 获取队列中数据,如果队列中没有数据,则返回null
    • 返回值E
  • poll(long timeout, TimeUnit unit)
    • 获取队列中数据,如果队列中没有数据,则阻塞当前线程timeout时间
    • 返回值E
  • take()
    • 获取队列中数据,如果队列中没有数据,会阻塞当前线程,直到有数据
    • 返回值E

三、源码(jdk1.8)

构造方法

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();
}
public ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) {
    this(capacity, fair);
    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();
    }
}

重要属性

// 数组
final Object[] items;

// 读Index
int takeIndex;

// 写Index
int putIndex;

// 当前数组中有多少个数据
int count;

// 锁
final ReentrantLock lock;

// Condition读
private final Condition notEmpty;

// Condition写
private final Condition notFull;

// 迭代器用的
transient Itrs itrs = null;

在这里插入图片描述

  • putIndex:记录下一次要给那个位置放数据
  • takeIndex:记录下一次要从那个位置获取数据
  • 如果index到达数组的尾端,那么就将index=0,这样其实就是实现了先进先出了啊

offer(E e)

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();
    }
}
private void enqueue(E x) {
    // 获得数组
    final Object[] items = this.items;
    // 将数据存放
    items[putIndex] = x;
    // 判断putIndex是不是到达数组尾端了
    if (++putIndex == items.length)
        // 到达尾端了那么就从头开始
        putIndex = 0;
    // 数组中数据数量加1
    count++;
    // 唤醒读线程
    notEmpty.signal();
}

add(E e)

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

offer(E e, long timeout, TimeUnit unit)

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);
        }
        enqueue(e);
        return true;
    } finally {
        lock.unlock();
    }
}

put(E e)

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();
    }
}

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;
    // 根据takeIndex获取数据
    E x = (E) items[takeIndex];
    // 然后将这个位置的数据放置为null
    items[takeIndex] = null;
    // 如果takeIndex到数组尾端了,那么就重置为0
    if (++takeIndex == items.length)
        takeIndex = 0;
    // 获取完数据,则count-1
    count--;
    // 迭代器
    if (itrs != null)
        itrs.elementDequeued();
    // 唤醒写线程
    notFull.signal();
    return x;
}

reomve()

public E remove() {
    E x = poll();
    if (x != null)
        return x;
    else
        throw new NoSuchElementException();
}

poll(E e, long timeout, TimeUnit unit)

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);
        }
        return dequeue();
    } finally {
        lock.unlock();
    }
}

take(E e)

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

四、总结流程(个人总结,可能有误)

  1. ArrayBlockingQueue其实就是用ReentrantLock+2个condition实现的生产消费队列啊。
    在这里插入图片描述
  2. offer/poll
    • offer:先看队列中的count跟items.length是否相等,如果相等,则返回false。不等则插入
    • poll:先看队列中的count==0,如果相等,则返回null。如果不等则返回数据呗
  3. offer(E e, long timeout, TimeUnit unit) / poll(E e, long timeout, TimeUnit unit)
    • offer:先看队列中的count跟items.length是否相等
      • 如果相等,则阻塞timeout时间,然后在唤醒,尝试一下。如果失败了就false。
      • 如果不等,则插入
    • poll:先看队列中的count==0
      • 如果相等,则阻塞timeout时间,然后在唤醒,尝试一下。如果失败了就返回null
      • 如果不等,则返回数据呗
  4. put/take
    • put:先看队列中的count跟items.length是否相等
      • 如果相等,则阻塞,直到被唤醒或者被打断
      • 如果不等,则插入
    • take:先看队列中的count==0
      • 如果相等,则阻塞,直到被唤醒或者被打断
      • 如果不等,则返回数据呗
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值