一:简介
阻塞队列:从定义上来说是队列的一种,那么肯定是一个先进先出(FIFO)的数据结构。与普通队列不同的是,它支持两个附加操作,即阻塞添加和阻塞删除方法。
阻塞添加:当阻塞队列是满时,往队列里添加元素的操作将被阻塞。
阻塞移除:当阻塞队列是空时,从队列中获取元素/删除元素的操作将被阻塞。
二:java中对阻塞队列的定义
BlockingQueue接口与Queue接口【Queue 和 BlockingQueue 都是在 Java 5 中加入的】
1:Queue接口
public interface Queue<E> extends Collection<E> {//添加一个元素,添加成功返回true, 如果队列满了,就会抛出异常boolean add(E e);//添加一个元素,添加成功返回true, 如果队列满了,返回falseboolean offer(E e);//返回并删除队首元素,队列为空则抛出异常E remove();//返回并删除队首元素,队列为空则返回nullE poll();//返回队首元素,但不移除,队列为空则抛出异常E element();//获取队首元素,但不移除,队列为空则返回nullE peek();
}
2:BlockingQueue接口
源码展示:
public interface BlockingQueue<E> extends Queue<E> {boolean add(E e);boolean offer(E e);void put(E e) throws InterruptedException;boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;E take() throws InterruptedException;E poll(long timeout, TimeUnit unit) throws InterruptedException;int remainingCapacity();boolean remove(Object o);publ