一个由数组结构组成的有界阻塞队列
构造方法
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
// 初始化数组,实例化ReentrantLock和两个等待队列notEmpty、notFull
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();
}
}
入队
// 添加元素,通过多态调用offer方法
public boolean add(E e) {
return super.add(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();
}
}
// 阻塞式添加元素,直至添加成功
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
// 如果数组已满,就将当前线程加入到notFull的等待队列
notFull.await();
// 添加元素到数组
enqueue(e);
} finally {
lock.unlock();
}
}
// 添加元素,超时后取消添加
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();
}
}
// 添加元素
private void enqueue(E x) {
// assert lock.getHoldCount() == 1;
// assert items[putIndex] == null;
final Object[] items = this.items;
// putIndex记录的是下一次将要添加的元素的索引位置
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
count++;
// 通知notEmpty的等待队列的线程可以取出元素
notEmpty.signal();
}
出队
// 取出元素,如果数组中没有元素,则返回null
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return (count == 0) ? null : dequeue();
} finally {
lock.unlock();
}
}
// 阻塞式获取元素,直至取出元素
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try
// 如果当前数组里元素个数为0,则当前线程进入到notEmpty的等待队列
while (count == 0)
notEmpty.await();
// 取出元素
return dequeue();
} finally {
lock.unlock();
}
}
// 取出元素,超时后取消添加
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
// 如果数组元素个数为0,就进入超时等待,如果超时了,就直接返回
while (count == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
// 取出元素
return dequeue();
} finally {
lock.unlock();
}
}
// 取出元素
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
@SuppressWarnings("unchecked")
// takeIndex表示下一次取出元素时的索引位置
E x = (E) items[takeIndex];
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
// 通知notFull的等待队列的线程可以添加元素
notFull.signal();
return x;
}
包含
// 包含
public boolean contains(Object o) {
if (o == null) return false;
final Object[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lock();
// 遍历从takeIndex到putIndex,如果存在该元素,则返回true
try {
if (count > 0) {
final int putIndex = this.putIndex;
int i = takeIndex;
do {
if (o.equals(items[i]))
return true;
if (++i == items.length)
i = 0;
} while (i != putIndex);
}
return false;
} finally {
lock.unlock();
}
}
删除
// 删除
public boolean remove(Object o) {
if (o == null) return false;
final Object[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lock();
// 从takeIndex到putIndex遍历,如果存在该元素,则删除
try {
if (count > 0) {
final int putIndex = this.putIndex;
int i = takeIndex;
do {
if (o.equals(items[i])) {
removeAt(i);
return true;
}
if (++i == items.length)
i = 0;
} while (i != putIndex);
}
return false;
} finally {
lock.unlock();
}
}