简介
ArrayBlockingQueue 字义理解就是 : 数组阻塞队列;看名字就很好理解!!
ArrayBlockingQueue 是有界队列,意思是队列个数不能超出某个数,超出入队阻塞,初始化就需要定义好个数,不能自动扩增或者修改。
上源码:
初始化
//初始化,定义临界值及是否公平两参数与
public ArrayBlockingQueue(int capacity, boolean fair) {
//临界值必须>0
if (capacity <= 0)
throw new IllegalArgumentException();
//初始化数组
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
//不空的标志
notEmpty = lock.newCondition();
//不满的标志
notFull = lock.newCondition();
}
入队
因为实现BlockingQueue 所以 ArrayBlockingQueue 也有add,put,offer入队方法
public boolean add(E e) {
//其实就是调用offer 超出个数 报异常
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();
}
}
private void enqueue(E x) {
final Object[] items = this.items;
//直接赋值
items[putIndex] = x;
//如果数组已经满了 ,重置putIndex 因为出队都是从0开始,
if (++putIndex == items.length)
putIndex = 0;
//入队次数
count++;
//通知不空阻塞 即 我有数据入队了
notEmpty.signal();
}
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();
}
}
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();
}
}
出队
//没有则返回空
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
//如果不空,则出队
return (count == 0) ? null : dequeue();
} finally {
lock.unlock();
}
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
//直接根据takeIndex 获取元素 将数组位置赋值为空
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null;
//超出循环 赋值为0
if (++takeIndex == items.length)
takeIndex = 0;
//减少个数,对应count++
count--;
if (itrs != null)
itrs.elementDequeued();
//通知不满条件 因为出队 肯定不满了
notFull.signal();
return x;
}
//必须等到元素出现
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
//如果为空,则 一直等到不空的通知
while (count == 0)
notEmpty.await();
//出队
return dequeue();
} finally {
lock.unlock();
}
}
//在一定时间内获取元素,过期返回null
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
//生成时间
long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
//如果为空 则等你个nanos时间,还没有?返回null
while (count == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
//如果有数据 则出队
return dequeue();
} finally {
lock.unlock();
}
}
//获取元素,但不移除,即当前待获取元素不变
public E peek() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return itemAt(takeIndex); // null when queue is empty
} 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();
try {
if (count > 0) {
final int putIndex = this.putIndex;
int i = takeIndex;
从//takeIndex 开始循环equals 找到
do {
if (o.equals(items[i])) {
//找到就移除
removeAt(i);
return true;
}
if (++i == items.length)
i = 0;
} while (i != putIndex);
}
return false;
} finally {
lock.unlock();
}
}
//移除某位置元素
void removeAt(final int removeIndex) {
// assert lock.getHoldCount() == 1;
// assert items[removeIndex] != null;
// assert removeIndex >= 0 && removeIndex < items.length;
final Object[] items = this.items;
if (removeIndex == takeIndex) {
// removing front item; just advance
//赋为null
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
} else {
// an "interior" remove
// slide over all others up through putIndex.
final int putIndex = this.putIndex;
//设置putIndex为removeIndex 即下次插入为这个位置,并且循环往前移动知道原先的putIndex位置的前一个位置
for (int i = removeIndex;;) {
int next = i + 1;
if (next == items.length)
next = 0;
if (next != putIndex) {
items[i] = items[next];
i = next;
} else {
items[i] = null;
this.putIndex = i;
break;
}
}
//减少个数
count--;
if (itrs != null)
itrs.removedAt(removeIndex);
}
//不满条件通知
notFull.signal();
}
总结
ArrayBlockingQueue 是有界队列,不能自动扩容,当时性能相较其他队列要稍微好点;
适用于确定队列个数或者某范围的队列!