目录
实现了 迭代器、集合、队列、阻塞队列、可序列化 接口
继承了 抽象集合、抽象队列 类
属性
代码块
// 序列化id
private static final long serialVersionUID = -817911632652898426L;
// 用数组 存储队列元素
final Object[] items;
// 下次获取元素的 下标
int takeIndex;
// 下次增加元素的 下标
int putIndex;
// 队列中元素的数目
int count;
// 主锁
final ReentrantLock lock;
// 非空等待condition
private final Condition notEmpty;
// 非满等待condition
private final Condition notFull;
// 迭代器
transient Itrs itrs = null;
构造方法:
代码块
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
// 初始化 底层数组存储结构、 lock 为公平锁、 condition
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;
// 队列满 则将写index置为0
putIndex = (i == capacity) ? 0 : i;
} finally {
lock.unlock();
}
}
add 方法: 如队列满或加入失败 抛异常
代码块
public boolean add(E e) {
// 调用父类add方法
return super.add(e);
}
public boolean add(E e) {
// 调用子类offer方法
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
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;
// put索引增加 如容器已满 索引置0
if (++putIndex == items.length)
putIndex = 0;
// 总数目增加
count++;
// 唤醒所有等待取数据的 wait节点
notEmpty.signal();
}
put方法:
代码块
public void put(E e) throws InterruptedException {
// 参数校验
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
// 如果容器已满
while (count == items.length)
// 执行await() 方法进行等待
notFull.await();
// 入队
enqueue(e);
} 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 {
// 如队列已满 执行带超时时间的wait方法
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 {
// 如果队列为空 返回null 否则返回头部元素
return (count == 0) ? null : dequeue();
} finally {
lock.unlock();
}
}
// 出队列方法 非原子操作 非线程安全
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
// 获取容器
final Object[] items = this.items;
@SuppressWarnings("unchecked")
// 获得当前队头元素
E x = (E) items[takeIndex];
// 删除队头元素
items[takeIndex] = null;
// 如果take索引 达到容器容量值 将其置0 循环队列
if (++takeIndex == items.length)
takeIndex = 0;
// 容器元素减1
count--;
// 如迭代器不为null
if (itrs != null)
// 迭代器元素出队列
itrs.elementDequeued();
// 唤醒 等待容器不满的所有节点
notFull.signal();
// 返回取出的元素
return x;
}
take 方法: 如队列中无元素 阻塞等待 直到队列中有元素
代码块
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
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 {
while (count == 0) {
// 超时直接返回null
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 itemAt(takeIndex); // null when queue is empty
} finally {
lock.unlock();
}
}
size:获取队列大小
代码块
public int size() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}