Queue常用类解析之PriorityQueue
Queue常用类解析之ConcurrentLinkedQueue
Queue常用类解析之BlockingQueue(一):PriorityBlockingQueue、DelayQueue和DelayedWorkQueue
接着上文对BlockingQueue的介绍继续向下
五、ArrayBlockingQueue
从命名可以看出,这是一个循环数组表示的的阻塞队列。
与前面介绍的BlockingQueue不同,ArrayBlockingQueue在入队和出队时都有可能会陷入阻塞。
1. 属性
/** The queued items */
final Object[] items;
/** items index for next take, poll, peek or remove */
int takeIndex;
/** items index for next put, offer, or add */
int putIndex;
/** Number of elements in the queue */
int count;
/*
* Concurrency control uses the classic two-condition algorithm
* found in any textbook.
*/
/** Main lock guarding all access */
final ReentrantLock lock;
/** Condition for waiting takes */
private final Condition notEmpty;
/** Condition for waiting puts */
private final Condition notFull;
/**
* Shared state for currently active iterators, or null if there
* are known not to be any. Allows queue operations to update
* iterator state.
*/
transient Itrs itrs = null;
putIndex和takeIndex分别表示入队和出队的数组索引。
notEmpty和notFull分别表示空队列和满队列时的阻塞condition。
Itrs 时迭代器的链表形式的集合。
2. 构造器
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();
}
ArrayBlockingQueue由两个构造器方法,不支持无参构造器。至少需要传入队列的容量,并初始化对于长度的数组。后续数组的长度无法修改。另外,ArrayBlockingQueue还支持在构造器方法中传入是否是公平锁的参数,默认是非公平锁。
3. ArrayBlockingQueue#put(Object)
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;
//putIndex入队的数组索引
items[putIndex] = x;
//循环数组,到达数组末尾后的下一个索引为0
if (++putIndex == items.length)
putIndex = 0;
count++;
//发送notEmpty信号唤醒
notEmpty.signal();
}
4. ArrayBlockingQueue#poll(long, TimeUnit)
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();
}
}
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;
//循环数组,达到数组末尾的下一个元素是0
if (++takeIndex == items.length)
takeIndex = 0;
count--;
//itrs操作
if (itrs != null)
itrs.elementDequeued();
//发送notFull信号唤醒线程
notFull.signal();
return x;
}
5. ArrayBlockingQueue#remove(Object)
public boolean remove(Object o) {
if (o == null) return false;
final Object[] items = this.items;
//加锁
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (count > 0) {
final int putIndex = this.putIndex;
int i = takeIndex;
//从takeIndex开始遍历,直到putIndex - 1
do {
//相等,执行删除逻辑
if (o.equals(items[i])) {
removeAt(i);
return true;
}
if (++i == items.length)