public class ArrayBlockingQueue
extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
| java.lang.Object | |||
| ↳ | java.util.AbstractCollection<E> | ||
| ↳ | java.util.AbstractQueue<E> | ||
| ↳ | java.util.concurrent.ArrayBlockingQueue<E> | ||
由数组支持的有界的blocking queue 。 该队列命令元素FIFO(先进先出)。 队列头是最长时间在队列中的元素。 队列的尾部是已经在队列上的最短时间的元素。 新元素插入到队列尾部,队列检索操作获取队列头部的元素。
这是一个经典的“有界缓冲区”,其中一个固定大小的数组包含生产者插入的元素,并由消费者提取。 一旦创建,容量不能改变。 尝试将put元素放入完整队列将导致操作阻塞; 尝试take来自空队列的元素将同样阻塞。
该类支持订购等待生产者和消费者线程的可选公平策略。 默认情况下,这个顺序不能保证。 但是,以公平性设置为true的队列授予按FIFO顺序的线程访问权限。 公平性通常会降低吞吐量,但会降低可变性并避免饥饿。
该类及其迭代器实现了 Collection和 Iterator接口的所有 可选方法。
Public constructors |
|
|---|---|
ArrayBlockingQueue(int capacity) 使用给定(固定)容量和默认访问策略创建 |
|
ArrayBlockingQueue(int capacity, boolean fair) 创建具有给定(固定)容量和指定访问策略的 |
|
ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c) 创建具有给定(固定)容量的 |
|
Public methods |
|
|---|---|
boolean |
add(E e) 在插入此队列的尾部,如果有可能立即这样做不超过该队列的容量,返回指定的元素 |
void |
clear() 从这个队列中删除所有元素。 |
boolean |
contains(Object o) 如果此队列包含指定的元素,则返回 |
int |
drainTo(Collection<? super E> c, int maxElements) 最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。 |
int |
drainTo(Collection<? super E> c) 从该队列中移除所有可用的元素,并将它们添加到给定的集合中。 |
Iterator<E> |
iterator() 以正确的顺序返回此队列中元素的迭代器。 |
boolean |
offer(E e) 插入指定的元素在这个队列的尾部,如果有可能立即这样做不超过该队列的容量,返回 |
boolean |
offer(E e, long timeout, TimeUnit unit) 在该队列的尾部插入指定的元素,如果队列已满,则等待指定的等待时间以使空间变为可用。 |
E |
peek() 检索但不移除此队列的头部,或者如果此队列为空,则返回 |
E |
poll(long timeout, TimeUnit unit) 检索并删除此队列的头部,如果元素变为可用,则等待达到指定的等待时间。 |
E |
poll() 检索并删除此队列的头部,或者如果此队列为空,则返回 |
void |
put(E e) 在队列的尾部插入指定的元素,等待队列满时空间变为可用。 |
int |
remainingCapacity() 返回此队列理想情况下(在没有内存或资源约束的情况下)无阻塞地接受的其他元素的数量。 |
boolean |
remove(Object o) 从该队列中移除指定元素的单个实例(如果存在)。 |
int |
size() 返回此队列中的元素数量。 |
Spliterator<E> |
spliterator() 返回此队列中元素的 |
E |
take() 检索并删除此队列的头部,如果需要等待,直到元素变为可用。 |
<T> T[] |
toArray(T[] a) 以适当的顺序返回包含此队列中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。 |
Object[] |
toArray() 以适当的顺序返回包含此队列中所有元素的数组。 |
String |
toString() 返回此集合的字符串表示形式。 |
Inherited methods |
|
|---|---|
java.util.AbstractQueue
|
|
java.util.AbstractCollection
|
|
java.lang.Object
|
|
java.util.Queue
|
|
java.util.Collection
|
|
java.util.concurrent.BlockingQueue
|
|
java.lang.Iterable
|
|
ArrayBlockingQueue (int capacity)
使用给定(固定)容量和默认访问策略创建 ArrayBlockingQueue 。
| Parameters | |
|---|---|
capacity |
int: the capacity of this queue |
| Throws | |
|---|---|
IllegalArgumentException |
if capacity < 1 |
ArrayBlockingQueue (int capacity,
boolean fair)
创建具有给定(固定)容量和指定访问策略的 ArrayBlockingQueue 。
| Parameters | |
|---|---|
capacity |
int: the capacity of this queue |
fair |
boolean: if true then queue accesses for threads blocked on insertion or removal, are processed in FIFO order; if false the access order is unspecified. |
| Throws | |
|---|---|
IllegalArgumentException |
if capacity < 1 |
ArrayBlockingQueue (int capacity,
boolean fair,
Collection<? extends E> c)
使用给定(固定)容量,指定的访问策略创建 ArrayBlockingQueue ,并初始包含给定集合的元素,并按集合迭代器的遍历顺序添加。
| Parameters | |
|---|---|
capacity |
int: the capacity of this queue |
fair |
boolean: if true then queue accesses for threads blocked on insertion or removal, are processed in FIFO order; if false the access order is unspecified. |
c |
Collection: the collection of elements to initially contain |
| Throws | |
|---|---|
IllegalArgumentException |
if capacity is less than c.size(), or less than 1. |
NullPointerException |
if the specified collection or any of its elements are null |
boolean add (E e)
在插入此队列的尾部,如果有可能立即这样做不超过该队列的容量,返回指定的元素 true成功时与抛出 IllegalStateException如果此队列已满。
| Parameters | |
|---|---|
e |
E: the element to add |
| Returns | |
|---|---|
boolean |
true (as specified by add(E)) |
| Throws | |
|---|---|
IllegalStateException |
if this queue is full |
NullPointerException |
if the specified element is null |
boolean contains (Object o)
如果此队列包含指定的元素,则返回true 。 更正式地,返回true当且仅当该队列包含至少一个元素e例如o.equals(e) 。
| Parameters | |
|---|---|
o |
Object: object to be checked for containment in this queue |
| Returns | |
|---|---|
boolean |
true if this queue contains the specified element |
int drainTo (Collection<? super E> c, int maxElements)
最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。 尝试将元素添加到集合c遇到的故障可能导致元素不在任何集合中,或者在引发关联的异常时集合都不集中。 尝试将队列IllegalArgumentException自身导致IllegalArgumentException 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| Parameters | |
|---|---|
c |
Collection: the collection to transfer elements into |
maxElements |
int: the maximum number of elements to transfer |
| Returns | |
|---|---|
int |
the number of elements transferred |
| Throws | |
|---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
|
int drainTo (Collection<? super E> c)
从该队列中移除所有可用的元素,并将它们添加到给定的集合中。 该操作可能比重复轮询该队列更有效。 尝试将元素添加到集合c遇到的故障可能导致元素不在任何集合中,或者在引发关联的异常时集合都不集中。 尝试将队列排空到自身导致IllegalArgumentException 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| Parameters | |
|---|---|
c |
Collection: the collection to transfer elements into |
| Returns | |
|---|---|
int |
the number of elements transferred |
| Throws | |
|---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
|
Iterator<E> iterator ()
以正确的顺序返回此队列中元素的迭代器。 元素将从第一个(头部)到最后一个(尾部)按顺序返回。
返回的迭代器是 weakly consistent 。
| Returns | |
|---|---|
Iterator<E> |
an iterator over the elements in this queue in proper sequence |
boolean offer (E e)
插入指定的元素在这个队列的尾部,如果有可能立即这样做不超过该队列的容量,返回true在成功和false如果此队列已满。 此方法通常比方法add(E) ,它只能通过抛出异常来插入元素。
| Parameters | |
|---|---|
e |
E: the element to add |
| Returns | |
|---|---|
boolean |
true if the element was added to this queue, else false |
| Throws | |
|---|---|
NullPointerException |
if the specified element is null |
boolean offer (E e,
long timeout,
TimeUnit unit)
在该队列的尾部插入指定的元素,如果队列已满,则等待指定的等待时间以使空间变为可用。
| Parameters | |
|---|---|
e |
E: the element to add |
timeout |
long: how long to wait before giving up, in units of unit |
unit |
TimeUnit: a TimeUnit determining how to interpret the timeout parameter |
| Returns | |
|---|---|
boolean |
true if successful, or false if the specified waiting time elapses before space is available |
| Throws | |
|---|---|
InterruptedException |
|
NullPointerException |
|
E peek ()
检索但不移除此队列的头部;如果此队列为空,则返回 null 。
| Returns | |
|---|---|
E |
the head of this queue, or null if this queue is empty |
E poll (long timeout,
TimeUnit unit)
检索并删除此队列的头部,如果元素变为可用,则等待达到指定的等待时间。
| Parameters | |
|---|---|
timeout |
long: how long to wait before giving up, in units of unit |
unit |
TimeUnit: a TimeUnit determining how to interpret the timeout parameter |
| Returns | |
|---|---|
E |
the head of this queue, or null if the specified waiting time elapses before an element is available |
| Throws | |
|---|---|
InterruptedException |
|
E poll ()
检索并删除此队列的头部,或者如果此队列为空,则返回 null 。
| Returns | |
|---|---|
E |
the head of this queue, or null if this queue is empty |
void put (E e)
在队列的尾部插入指定的元素,等待队列满时空间变为可用。
| Parameters | |
|---|---|
e |
E: the element to add |
| Throws | |
|---|---|
InterruptedException |
|
NullPointerException |
|
int remainingCapacity ()
返回此队列理想情况下(在没有内存或资源约束的情况下)无阻塞地接受的其他元素的数量。 这总是等于此队列的初始容量减去此队列的当前size 。
请注意,您 不能总是通过检查 remainingCapacity来判断尝试插入元素是否成功,因为可能是另一个线程即将插入或移除元素。
| Returns | |
|---|---|
int |
the remaining capacity |
boolean remove (Object o)
从该队列中移除指定元素的单个实例(如果存在)。 更正式地说,删除一个元素e ,使得o.equals(e) ,如果这个队列包含一个或多个这样的元素。 如果此队列包含指定的元素(或者等同于此队列因呼叫而更改),则返回true 。
在基于循环阵列的队列中去除内部元素本质上是一种缓慢且具有破坏性的操作,因此只能在特殊情况下进行,理想情况下只有当队列已知不能被其他线程访问时。
| Parameters | |
|---|---|
o |
Object: element to be removed from this queue, if present |
| Returns | |
|---|---|
boolean |
true if this queue changed as a result of the call |
Spliterator<E> spliterator ()
返回此队列中元素的 Spliterator 。
返回的分隔符是 weakly consistent 。
该 Spliterator报告 CONCURRENT , ORDERED ,并 NONNULL 。
Spliterator implements trySplit to permit limited parallelism.| Returns | |
|---|---|
Spliterator<E> |
a Spliterator over the elements in this queue |
E take ()
检索并删除此队列的头部,如果需要等待,直到元素变为可用。
| Returns | |
|---|---|
E |
the head of this queue |
| Throws | |
|---|---|
InterruptedException |
|
T[] toArray (T[] a)
以适当的顺序返回包含此队列中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。 如果队列适合指定的数组,则返回其中。 否则,将使用指定数组的运行时类型和此队列的大小分配一个新数组。
如果此队列适合指定阵列,并有空余空间(即阵列具有比此队列更多的元素),则紧随队列尾部的阵列中的元素将设置为 null 。
与toArray()方法一样,此方法充当基于数组和基于集合的API之间的桥梁。 此外,该方法允许精确控制输出数组的运行时类型,并且在某些情况下可以用于节省分配成本。
假设x是已知只包含字符串的队列。 以下代码可用于将队列转储到新分配的String阵列中:
String[] y = x.toArray(new String[0]); Note that
toArray(new Object[0]) is identical in function to
toArray().
| Parameters | |
|---|---|
a |
T: the array into which the elements of the queue are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose |
| Returns | |
|---|---|
T[] |
an array containing all of the elements in this queue |
| Throws | |
|---|---|
ArrayStoreException |
if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue |
NullPointerException |
if the specified array is null |
Object[] toArray ()
以适当的顺序返回包含此队列中所有元素的数组。
返回的数组将是“安全”的,因为此队列不维护对它的引用。 (换句话说,这个方法必须分配一个新的数组)。 调用者可以自由修改返回的数组。
此方法充当基于数组和基于集合的API之间的桥梁。
| Returns | |
|---|---|
Object[] |
an array containing all of the elements in this queue |
String toString ()
返回此集合的字符串表示形式。 字符串表示由集合元素的列表组成,它们按其迭代器返回的顺序包含在方括号中( "[]" )。 相邻元素由字符", " (逗号和空格)分隔。 通过valueOf(Object)将元素转换为字符串。
| Returns | |
|---|---|
String |
a string representation of this collection |