前言
说到队列,大家都不陌生,很多场景都需要使用到队列,今天我们一起学习JDK提供的队列源码
类图

 Queue接口的实现类有很多
 
 从中,我们看到几个熟悉的,BlockingQueue 阻塞队列、Deque 双端队列
Queue
官方介绍:
A collection designed for holding elements prior to processing.
Besides basic {@link java.util.Collection Collection} operations,
queues provide additional insertion, extraction, and inspection
operations. Each of these methods exists in two forms: one throws
an exception if the operation fails, the other returns a special
value (either {@code null} or {@code false}, depending on the
operation). The latter form of the insert operation is designed
specifically for use with capacity-restricted {@code Queue}
implementations; in most implementations, insert operations cannot
fail.
用于在处理前保存元素的集合。除了基本的Collection操作外,队列还提供额外的插入、提取和检查操作。
 这些方法都以两种形式存在:
 一种在操作失败时抛出异常,另一种返回特殊的值 null 或 false,取决于操作后一种形式的插入操作是专门为有容量限制的{@code Queue} 实现设计的;在大多数实现中,插入操作不会失败
接口方法
- add() : 添加元素,成功返回true,队列满时抛出异常
- offer() : 添加元素,队列满时返回false
- remove() : 从头部删除队列元素并返回该元素,队列空时抛出异常
- peek() : 返回队列头部元素不删除,队列空时返回null
- element() : 与peek()方法一样,队列空时抛出异常
BlockingQueue
阻塞队列接口,继承Queue接口
 方法特性:
 1.抛出异常
 2.存储元素
 3.阻塞
 4.超时
- 插入元素:
 add(e)、offer(e)、put(e)、offer(e,time,unit)
- 移除元素:
 remove()、poll()、take()、poll(time,unit)
- 检出元素:
 element()、peek()
方法列表:

相比Queue接口,重载了offer和poll 方法,新增加了take、put等方法
接口方法
重点看一下重载的offer和poll方法,及新增的take和put方法
-  boolean offer(E e, long timeout, TimeUnit unit) 
 throws InterruptedException;
 往队列添加一个元素,队列满时,阻塞等待timeout时间,成功返回true,失败返回false,不能添加null元素
-  E poll(long timeout, TimeUnit unit) throws InterruptedException; 
 检出并删除队列头部元素,队列空时,阻塞等待timeout时间,成功返回头部元素,失败返回null
-  E take() throws InterruptedException; 
 检索并删除队列头部元素,队列空时,阻塞等待有元素为止
-  void put(E e) throws InterruptedException; 
 添加元素到队列中,队列满时,阻塞等待可用空间



















