BlockingQueue是juc包提供的用于解决并发 生产者-消费者最有用的类,他的特性是在任意时刻只有一个线程可以进行take或者put操作,并且BlockingQueue提供了超时return null的机制,很多生成场景里都可以看到这个工具。
ArrayBlockingQueue队列基于数组实现,容量大小在创建ArrayBlockingQueue对象时已定义好
应用场景:在线程池中有比较多的应用,生产者消费者中。
他是基于ReentrantLock保证线程安全的,根据Condition实现队列满时的阻塞
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} 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();
enqueue(e);
} finally {
lock.unlock();
}
}



