在线程池创建的时候,需要传一个堵塞队列来维护需要执行的线程任务,其中最常用的是ArrayBlockingQueue和LinkedBlockingQueue。他们都继承了BlockingQueue接口。
ArrayBlockingQueue
一个有边界的堵塞队列,内部使用了一个队列来保存元素,有takeIndex和putIndex来维护队列头和尾部的游标。
/** 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;
/** Main lock guarding all access */
//在取和存元素的时候会进行加锁
final ReentrantLock lock;
/** Condition for waiting takes */
//在take的时候,如果数组为空,进行堵塞,直到数组不为空
private final Condition notEmpty;
/** Condition for waiting puts */
//在poll的时候,如果数组满了,进行堵塞,直到数组有空位
private final Condition notFull;
public ArrayBlockingQueue(int capacity, boolean fair) {
if<