public class ArrayBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
//用于存储元素的数组
final Object[] items;
// 下一个取出元素的坐标
int takeIndex;
// 可以添加元素的坐标
int putIndex;
// 队列中元素的数量
int count;
//队列插入和删除元素锁
final ReentrantLock lock;
//生产者信号量,如果取的时候为空,等待
private final Condition notEmpty;
//消费者信号量,如果放入元素队列满,等待
private final Condition notFull;
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();
}
//循环加1
final int inc(int i) {
return (++i == items.length) ? 0 : i;
}
//循环减1
final int inc(int i) {
return (++i == items.length) ? 0 : i;
}
//插入元素
private void insert(E x) {
items[putIndex] = x;
putIndex = inc(putIndex); //索引位置加1
++count; //总数量加1
notEmpty.signal(); //唤醒,等待数据出队的线程
}
//元素出队
private E extract() {
final Object[] items = this.items;
E x = this.<E>cast(items[takeIndex]);
items[takeIndex] = null;
takeIndex = inc(takeIndex); //取索引加1
--count; //总数量减1
notFull.signal(); //唤醒,等待数据入队的线程
return x;
}
//入队,非阻塞,失败抛异常
public boolean add(E e) {
return super.add(e); //最后调用还是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 {
insert(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.await(); //阻塞等待
insert(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); //阻塞等待一段时间
}
insert(e); //插入元素
return true;
} finally {
lock.unlock();
}
}
//出队,非阻塞
public E poll() {
final ReentrantLock lock = this.lock;
lock.lock(); //加锁
try {
return (count == 0) ? null : extract(); //队列为空返回,否则出队
} finally {
lock.unlock();
}
}
//出对,阻塞并响应中断
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly(); //加锁,响应中断
try {
while (count == 0) //队列为空
notEmpty.await(); //出队等待
return extract(); //出队
} 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 {
while (count == 0) { //队列为空
if (nanos <= 0) //直接返回null
return null;
nanos = notEmpty.awaitNanos(nanos); //阻塞等待一段时间
}
return extract(); //出队
} finally {
lock.unlock();
}
}
//返回队头元素,不从队列中删除
public E peek() {
final ReentrantLock lock = this.lock;
lock.lock(); //加锁
try {
return (count == 0) ? null : itemAt(takeIndex); //队列为空返回null,否则直接下1个元素
} finally {
lock.unlock();
}
}
//删除指定索引位置的元素
void removeAt(int i) {
final Object[] items = this.items;
if (i == takeIndex) { //删除索引和takeIndex相同,直接从当前位置删除
items[takeIndex] = null;
takeIndex = inc(takeIndex);
} else { //否则遍历
for (;;) { // 否则将删除位置之后的元素都向前移一位
int nexti = inc(i);
if (nexti != putIndex) {
items[i] = items[nexti];
i = nexti;
} else {
items[i] = null;
putIndex = i;
break;
}
}
}
--count; //总数量减1
notFull.signal(); //唤醒因为队列满,插入数据阻塞的线程
}
//删除某个指定的元素
public boolean remove(Object o) {
if (o == null) return false;
final Object[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lock();
try {
for (int i = takeIndex, k = count; k > 0; i = inc(i), k--) {
if (o.equals(items[i])) { //找到指定元素
removeAt(i); //删除指定位置元素
return true;
}
}
return false;
} finally {
lock.unlock();
}
}
//获取剩余容量
public int remainingCapacity() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return items.length - count; //返回剩余容量
} finally {
lock.unlock();
}
}
}
ArrayBlockingQueue代码阅读
最新推荐文章于 2021-03-05 18:06:41 发布