1、LinkedBlockingQueue
(1)、首先来看看LinkedBlockingQueue类继承了那些类和实现了那些接口
基于链接节点的可选限定的blocking queue 。 这个队列排列元素FIFO(先进先出)。 队列的头部是队列中最长的元素。 队列的尾部是队列中最短时间的元素。 新元素插入队列的尾部,队列检索操作获取队列头部的元素。 链接队列通常具有比基于阵列的队列更高的吞吐量,但在大多数并发应用程序中的可预测性能较低。
(2)、LinkedBlockingQueue的构造方法
/**
* 创建一个LinkedBlockingQueue ,容量为Integer.MAX_VALUE
* Creates a {@code LinkedBlockingQueue} with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
/**
* Creates a {@code LinkedBlockingQueue} with the given (fixed) capacity.
* 创建一个LinkedBlockingQueue ,容量为capacity
* @param capacity the capacity of this queue
* @throws IllegalArgumentException if {@code capacity} is not greater
* than zero
*/
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
/**
* 创建一个LinkedBlockingQueue ,容量为Integer.MAX_VALUE,最初包含给定集合的元素,以集合的 迭代器的遍历顺序添加。
* Creates a {@code LinkedBlockingQueue} with a capacity of
* {@link Integer#MAX_VALUE}, initially containing the elements of the
* given collection,
* added in traversal order of the collection's iterator.
*
* @param c the collection of elements to initially contain
* @throws NullPointerException if the specified collection or any
* of its elements are null
*/
public LinkedBlockingQueue(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
final ReentrantLock putLock = this.putLock;
putLock.lock(); // Never contended, but necessary for visibility
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();
}
}
(3)、常用方法
方法 | 作用 |
boolean add(E e) | 在此qeque的末尾插入指定的元素,除非它会违反容量限制(容量不足时抛出IllegalStateException异常)。 |
boolean offer(E e) | 将指定的元素插入由此queue表示的队列(换句话说,在该queue的尾部),如果可以立即执行,而不违反容量限制, true在成功时 false如果当前没有可用空间,则返回false |
boolean offer(E e, long timeout, TimeUnit unit) | 将指定的元素插入由此queue表示的队列中(换句话说,在该queue的尾部),等待指定的等待时间(如果需要空间可用),会造成阻塞。 |
void clear() | 从这个queue原子地删除所有的元素。 |
E remove() | 检索并删除由此queue表示的队列的头部。 |
E take() | 检索并删除由此queue(换句话说,该queue的第一个元素)表示的队列的头部,如果需要,等待,直到元素可用。 |
E peek() | 检索但不删除由此queue表示的队列的头部(换句话说,此queue的第一个元素),如果此queue为空,则返回 null 。 |
E poll() | 检索并删除由此queue表示的队列的头部(换句话说,该queue的第一个元素),如果此queue为空,则返回 null 。 |
E poll(long timeout, TimeUnitunit) | 检索并删除由此queue(换句话说,该queue的第一个元素)表示的队列的头部,等待到指定的等待时间(如有必要)使元素变为可用 |
int size() | 返回此queue中的元素数 |
boolean contains(Object o) | 如果此queue包含指定的元素,则返回 true |
E pop() | 从这个queue表示的堆栈中弹出一个元素。 |
void push(E e) | 将元素推入此双端队列表示的堆栈(换句话说,在该双端队列的头部),如果它是立即可行且不会违反容量限制,抛出IllegalStateException如果当前没有空间可用。 |
void put(E e) | 将指定的元素插入由此queue表示的队列(换句话说,在该queue的尾部),等待空格变为可用时。 |
2、ArrayBlockingQueue
(1)、ArrayBlockingQueue继承了那些类 和 实现了那些接口
一个有限的blocking queue由数组支持.这个队列排列元素FIFO(先进先出)。队列的头部是队列中最长的元素。队列的尾部是队列中最短时间的元素。新元素插入队列的尾部,队列检索操作获取队列头部的元素。 这是一个经典的”有界缓冲区“,其中固定大小的数组保存由生产者插入的元素并由消费者提取。创建后,容量无法更改。 尝试put成满的队列的元件将导致在操作阻挡; 尝试take从空队列的元件将类似地阻塞。 此类支持可选的公平策略,用于订购等待的生产者和消费者线程。默认情况下,此订单不能保证。 然而,以公平设置为true的队列以FIFO顺序授予线程访问权限。公平性通常会降低吞吐量,但会降低变异性并避免饥饿。 该类及其迭代器实现了Collection和Iterator接口的所有可选方法。
(2)、ArrayBlockingQueue的几个构造方法
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and default access policy.
*
* @param capacity the capacity of this queue
* @throws IllegalArgumentException if {@code capacity < 1}
*/
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and the specified access policy.
*
* @param capacity the capacity of this queue
* @param fair if {@code true} then queue accesses for threads blocked
* on insertion or removal, are processed in FIFO order;
* if {@code false} the access order is unspecified.
* @throws IllegalArgumentException if {@code capacity < 1}
*/
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();
}
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity, the specified access policy and initially containing the
* elements of the given collection,
* added in traversal order of the collection's iterator.
*
* @param capacity the capacity of this queue
* @param fair if {@code true} then queue accesses for threads blocked
* on insertion or removal, are processed in FIFO order;
* if {@code false} the access order is unspecified.
* @param c the collection of elements to initially contain
* @throws IllegalArgumentException if {@code capacity} is less than
* {@code c.size()}, or less than 1.
* @throws NullPointerException if the specified collection or any
* of its elements are null
*/
public ArrayBlockingQueue(int capacity, boolean fair,
Collection<? extends E> c) {
this(capacity, fair);
final ReentrantLock lock = this.lock;
lock.lock(); // Lock only for visibility, not mutual exclusion
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();
}
}
(3)、常用方法
方法 | 作用 |
boolean add(E e) | 在插入此队列的尾部,如果有可能立即这样做不超过该队列的容量,返回指定的元素 true成功时与抛出 IllegalStateException如果此队列已满。 |
void put(E e) | 在该队列的尾部插入指定的元素,如果队列已满,则等待空间变为可用。 |
boolean offer(E e) | 如果可以在不超过队列容量的情况下立即将其指定的元素插入该队列的尾部,则在成功时 false如果该队列已满,则返回 true 。 |
boolean offer(E e, long timeout, TimeUnit unit) | 在该队列的尾部插入指定的元素,等待指定的等待时间,以使空间在队列已满时变为可用。 |
E peek() | 检索但不删除此队列的头,如果此队列为空,则返回 null 。 |
E poll() | 检索并删除此队列的头,如果此队列为空,则返回 null 。 |
E poll(long timeout, TimeUnit unit) | 检索并删除此队列的头,等待指定的等待时间(如有必要)使元素变为可用。 |
E take() | 检索并删除此队列的头,如有必要,等待元素可用。 从这个队列中原子地删除所有的元素。 |
boolean contains(Object o) | 如果此队列包含指定的元素,则返回 true 。 |
int size() | 返回此队列中的元素数。 |