1 Queue<E>接口
public interface Queue<E> extends Collection<E> {
// 插入指定元素
// 插入成功返回true;如果隊(duì)列沒(méi)有可用空間,拋出異常
boolean add(E e);
// 插入指定元素
// 插入成功返回true;如果隊(duì)列沒(méi)有可用空間,返回false
boolean offer(E e);
// 獲取和刪除頭部元素
// 如果隊(duì)列中沒(méi)有元素,拋出異常
E remove();
// 獲取和刪除頭部元素
// 如果隊(duì)列中沒(méi)有元素,返回null
E poll();
// 獲取頭部元素
// 如果隊(duì)列中沒(méi)有元素,拋出異常
E element();
// 獲取頭部元素
// 如果隊(duì)列中沒(méi)有元素,返回null
E peek();
}
2 BlockingQueue<E>接口
public interface BlockingQueue<E> extends Queue<E> {
// 插入指定元素
// 插入成功返回true;如果隊(duì)列沒(méi)有可用空間,拋出異常
boolean add(E e);
// 插入指定元素
// 插入成功返回true;如果隊(duì)列沒(méi)有可用空間,返回false
boolean offer(E e);
// 插入指定元素
// 插入成功直接返回;如果隊(duì)列沒(méi)有可用空間,阻塞當(dāng)前線程
void put(E e) throws InterruptedException;
// 插入指定元素
// 插入成功返回true;如果隊(duì)列沒(méi)有可用空間,阻塞當(dāng)前線程;如果在指定時(shí)間之內(nèi)隊(duì)列一直沒(méi)有可用空間,返回false
boolean offer(E e, long timeout, TimeUnit unit)
throws InterruptedException;
// 獲取和刪除頭部元素
// 如果隊(duì)列中沒(méi)有元素,阻塞當(dāng)前線程
E take() throws InterruptedException;
// 獲取和刪除頭部元素
// 如果隊(duì)列中沒(méi)有元素,阻塞當(dāng)前線程;如果在指定時(shí)間之內(nèi)隊(duì)列中一直沒(méi)有元素,返回null
E poll(long timeout, TimeUnit unit)
throws InterruptedException;
// 獲取隊(duì)列的剩余容量
int remainingCapacity();
// 刪除指定元素
// 如果隊(duì)列發(fā)生改變,返回true
boolean remove(Object o);
// 判斷隊(duì)列中是否包含指定元素
public boolean contains(Object o);
// 刪除隊(duì)列中的全部元素,并將這些元素添加到指定集合中
int drainTo(Collection<? super E> c);
// 刪除隊(duì)列中指定數(shù)量的元素,并將這些元素添加到指定集合中
int drainTo(Collection<? super E> c, int maxElements);
}
3 ArrayBlockingQueue
3.1 ArrayBlockingQueue的特點(diǎn)
(1)數(shù)據(jù)結(jié)構(gòu):數(shù)組。
(2)有界無(wú)界:有界。
(3)出隊(duì)入隊(duì):FIFO。
3.2 ArrayBlockingQueue的構(gòu)造方法
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
// 傳入的fair用于創(chuàng)建ReentrantLock
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();
}
// 傳入的fair用于創(chuàng)建ReentrantLock
public ArrayBlockingQueue(int capacity, boolean fair,
Collection<? extends E> c) {
this(capacity, fair);
final ReentrantLock lock = this.lock;
lock.lock(); // 獲取鎖,保證可見(jiàn)性
try {
int i = 0;
try {
for (E e : c) {
checkNotNull(e);
items[i++] = e;
}
} catch (ArrayIndexOutOfBoundsException ex) {
throw new IllegalArgumentException();
}
count = i;
putIndex = (i == capacity) ? 0 : i;
} finally {
lock.unlock();
}
}
4 LinkedBlockingQueue
4.1 LinkedBlockingQueue的特點(diǎn)
(1)數(shù)據(jù)結(jié)構(gòu):?jiǎn)蜗蜴湵怼?br>
(2)有界無(wú)界:可選。
(3)出隊(duì)入隊(duì):FIFO。
4.2 LinkedBlockingQueue的構(gòu)造方法
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
public LinkedBlockingQueue(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
final ReentrantLock putLock = this.putLock;
putLock.lock(); // 獲取鎖,保證可見(jiàn)性
try {
int n = 0;
for (E e : c) {
if (e == null)
throw new NullPointerException();
if (n == capacity)
throw new IllegalStateException("Queue full");
enqueue(new Node<E>(e));
++n;
}
count.set(n);
} finally {
putLock.unlock();
}
}
5 SynchronousQueue
5.1 SynchronousQueue的特點(diǎn)
(1)SynchronousQueue是一個(gè)特殊的隊(duì)列,它不存儲(chǔ)任何元素。
(2)一個(gè)線程向隊(duì)列中添加元素,不會(huì)立即返回,直至另一個(gè)線程從隊(duì)列中獲取元素;一個(gè)線程從隊(duì)列中獲取元素,不會(huì)立即返回,直至另一個(gè)線程向隊(duì)列中添加元素。
(3)這里的Synchronous是指讀線程和寫(xiě)線程同步。
5.2 SynchronousQueue中的構(gòu)造方法
public SynchronousQueue() {
this(false);
}
public SynchronousQueue(boolean fair) {
transferer = fair ? new TransferQueue<E>() : new TransferStack<E>();
}
6 PriorityBlockingQueue
6.1 PriorityBlockingQueue的特點(diǎn)
(1)數(shù)據(jù)結(jié)構(gòu):數(shù)組。
(2)有界無(wú)界:無(wú)界。支持自動(dòng)擴(kuò)容,最大容量為Integer.MAX_VALUE - 8。
(3)出隊(duì)入隊(duì):優(yōu)先級(jí)大小。
6.2 PriorityBlockingQueue中的構(gòu)造方法
// DEFAULT_INITIAL_CAPACITY為11
public PriorityBlockingQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}
public PriorityBlockingQueue(int initialCapacity) {
this(initialCapacity, null);
}
public PriorityBlockingQueue(int initialCapacity,
Comparator<? super E> comparator) {
if (initialCapacity < 1)
throw new IllegalArgumentException();
this.lock = new ReentrantLock();
this.notEmpty = lock.newCondition();
this.comparator = comparator;
this.queue = new Object[initialCapacity];
}
public PriorityBlockingQueue(Collection<? extends E> c) {
this.lock = new ReentrantLock();
this.notEmpty = lock.newCondition();
// heapify表示是否進(jìn)行堆排序
boolean heapify = true;
// screen表示是否檢查集合中的元素
boolean screen = true;
if (c instanceof SortedSet<?>) {
SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
this.comparator = (Comparator<? super E>) ss.comparator();
heapify = false;
}
else if (c instanceof PriorityBlockingQueue<?>) {
PriorityBlockingQueue<? extends E> pq =
(PriorityBlockingQueue<? extends E>) c;
this.comparator = (Comparator<? super E>) pq.comparator();
screen = false;
// 如果類型匹配
if (pq.getClass() == PriorityBlockingQueue.class)
heapify = false;
}
Object[] a = c.toArray();
int n = a.length;
// If c.toArray incorrectly doesn't return Object[], copy it.
if (a.getClass() != Object[].class)
a = Arrays.copyOf(a, n, Object[].class);
// 如果集合是PriorityBlockingQueue,不檢查集合中的元素
// 如果集合是SortedSet并且集合中只有一個(gè)元素,檢查集合中的元素
// 如果集合是SortedSet并且集合中含有多個(gè)元素并且SortedSet中的comparator不等于null,檢查集合中的元素
// 如果集合是SortedSet并且集合中含有多個(gè)元素并且SortedSet中的comparator等于null,不檢查集合中的元素
// 如果集合既不是PriorityBlockingQueue又不是SortedSet并且集合中只有一個(gè)元素,檢查集合中的元素
// 如果集合既不是PriorityBlockingQueue又不是SortedSet并且集合中含有多個(gè)元素,不檢查集合中的元素
if (screen && (n == 1 || this.comparator != null)) {
for (int i = 0; i < n; ++i)
if (a[i] == null)
throw new NullPointerException();
}
this.queue = a;
this.size = n;
if (heapify)
heapify();
}
7 DelayQueue
一個(gè)使用優(yōu)先級(jí)隊(duì)列實(shí)現(xiàn)的無(wú)界阻塞隊(duì)列。
public DelayQueue() {}
public DelayQueue(Collection<? extends E> c) {
this.addAll(c);
}
8 LinkedTransferQueue
一個(gè)由鏈表結(jié)構(gòu)組成的無(wú)界阻塞隊(duì)列。
9 LinkedBlockingDeque
一個(gè)由鏈表結(jié)構(gòu)組成的雙向阻塞隊(duì)列。
public LinkedBlockingDeque() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingDeque(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
}
public LinkedBlockingDeque(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
final ReentrantLock lock = this.lock;
lock.lock(); // 獲取鎖,保證可見(jiàn)性
try {
for (E e : c) {
if (e == null)
throw new NullPointerException();
if (!linkLast(new Node<E>(e)))
throw new IllegalStateException("Deque full");
}
} finally {
lock.unlock();
}
}
ArrayBlockingQueue 底層是數(shù)組,有界隊(duì)列,如果我們要使用生產(chǎn)者-消費(fèi)者模式,這是非常好的選擇。
LinkedBlockingQueue 底層是鏈表,可以當(dāng)做無(wú)界和有界隊(duì)列來(lái)使用,所以大家不要以為它就是無(wú)界隊(duì)列。
SynchronousQueue 本身不帶有空間來(lái)存儲(chǔ)任何元素,使用上可以選擇公平模式和非公平模式。
PriorityBlockingQueue 是無(wú)界隊(duì)列,基于數(shù)組,數(shù)據(jù)結(jié)構(gòu)為二叉堆,數(shù)組第一個(gè)也是樹(shù)的根節(jié)點(diǎn)總是最小值。