核心变量
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
// 底层存储:定长数组,一旦初始化容量不可扩容
final Object[] items;
// 出队索引:take/poll 取元素位置
int takeIndex;
// 入队索引:put/offer 添加元素位置
int putIndex;
// 当前队列元素总数
int count;
// 全局独占锁:所有入队、出队操作共用一把锁(核心特性!单锁)
final ReentrantLock lock;
// 非空条件:队列有元素时唤醒消费者
private final Condition notEmpty;
// 非满条件:队列有空位时唤醒生产者
private final Condition notFull;
}
构造方法
- 数组固定容量,无扩容机制,满了就阻塞 / 抛异常
- 入队、出队、查询全部竞争同一把 ReentrantLock
// 1. 基础构造:指定容量,默认非公平锁
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
// 2. 完整构造:容量 + 是否公平锁
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
// 初始化定长数组,容量固定
items = new Object[capacity];
// 创建可重入锁,fair=true公平,false非公平(默认)
lock = new ReentrantLock(fair);
// 绑定两个条件变量,共用同一把lock
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
// 3. 带集合初始化的构造
public ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) {
this(capacity, fair);
// 加锁批量导入集合元素
final ReentrantLock lock = this.lock;
lock.lock();
try {
int i = 0;
for (E e : c) {
checkNotNull(e);
items[i++] = e;
}
count = i;
putIndex = (i == capacity) ? 0 : i;
} finally {
lock.unlock();
}
}
入队方法
add
- 调用 offer,失败直接抛队列满异常,不阻塞
public boolean add(E e) {
// 调用父类AbstractQueue的add
return super.add(e);
}
// AbstractQueue.add
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
offer(不超时)
- 加锁,队列满直接返回 false,不阻塞
public boolean offer(E e) {
checkNotNull(e); // 不允许null元素
final ReentrantLock lock = this.lock;
lock.lock();
try {
// 判断队列未满才入队
if (count == items.length)
return false;
else {
enqueue(e);
return true;
}
} finally {
lock.unlock();
}
}
offer(超时)
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;
// 生产者等待notFull,释放锁,超时唤醒
nanos = notFull.awaitNanos(nanos);
}
enqueue(e);
return true;
} finally {
lock.unlock();
}
}
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();
}
}
enqueue
- 环形数组,putIndex 循环 0 ~ (length - 1)
private void enqueue(E x) {
// putIndex位置放入元素
final Object[] items = this.items;
items[putIndex] = x;
// 环形数组:到末尾重置索引为0
if (++putIndex == items.length)
putIndex = 0;
count++;
// 放入元素后,唤醒一个阻塞的消费者
notEmpty.signal();
}
对比
| 方法 | 队列满时的动作 | 返回值 | 是否阻塞 |
|---|---|---|---|
| add(e) | 直接失败 | 抛 IllegalStateException | 不阻塞 |
| offer(e) | 直接失败 | return false | 不阻塞 |
| put(e) | 阻塞等待空位 | 无返回 | 无限阻塞,可中断(notFull.await) |
| offer(e,time,unit) | 限时阻塞 | 超时返回 false | 限时阻塞(notFull.awaitNanos) |
出队方法
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();
}
}
poll
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
// 空队列循环等待,超时返回null
while (count == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
return dequeue();
} finally {
lock.unlock();
}
}
peek
public E peek() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return (count == 0) ? null : itemAt(takeIndex);
} finally {
lock.unlock();
}
}
dequeue
private E dequeue() {
final Object[] items = this.items;
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
// 清空当前位置,帮助GC
items[takeIndex] = null;
// 环形索引重置
if (++takeIndex == items.length)
takeIndex = 0;
count--;
// 取出元素,唤醒阻塞生产者
notFull.signal();
return x;
}
对比
| 方法 | 队列为空行为 | 返回值 | 是否阻塞 |
|---|---|---|---|
| take() | 返回元素 无限阻塞 | 返回元素 | 无限阻塞,可中断 |
| poll() | 直接返回 null | 返回元素 | 不阻塞 |
| poll(time,unit) | 限时阻塞 | 超时返回 null | 限时阻塞 |
| peek() | 返回 null | 返回元素 | 不阻塞 |
总结
-
底层结构:定长环形数组 + 单 ReentrantLock + 两个 Condition;
-
锁机制:所有读写操作竞争同一把锁,同一时刻只能有一个线程操作队列,读写互斥;
-
环形索引:takeIndex、putIndex 循环递增,到达数组尾部归零,避免数组迁移;
-
等待唤醒逻辑
- 生产者满了:notFull.await(),消费后 notFull.signal();
- 消费者空了:notEmpty.await(),生产后 notEmpty.signal();
-
不允许 null:所有入队操作先执行checkNotNull(e),空元素直接抛 NPE;
-
无扩容:初始化容量固定,是有界阻塞队列。
828

被折叠的 条评论
为什么被折叠?



