public class LinkedBlockingQueue
extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
| java.lang.Object | |||
| java.util.AbstractCollection<E> | |||
| java.util.AbstractQueue<E> | |||
| java.util.concurrent.LinkedBlockingQueue<E> | |||
基于链接节点的可选有界blocking queue 。 该队列命令元素FIFO(先进先出)。 队列头是最长时间在队列中的元素。 队列的尾部是已经在队列上的最短时间的元素。 新元素插入到队列尾部,队列检索操作获取队列头部的元素。 链接队列通常比基于阵列的队列具有更高的吞吐量,但在大多数并发应用程序中性能较差。
可选的容量绑定构造函数参数可用作防止队列过度扩展的一种方法。 如果未指定,容量等于MAX_VALUE 。 链接节点在每次插入时动态创建,除非这会使队列超出容量。
该类及其迭代器实现了 Collection和 Iterator接口的所有 可选方法。
Public constructors |
|
|---|---|
LinkedBlockingQueue() 创建 |
|
LinkedBlockingQueue(int capacity) 用给定(固定)容量创建一个 |
|
LinkedBlockingQueue(Collection<? extends E> c) 创建 |
|
公共方法(Public methods) |
|
|---|---|
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
|
|
LinkedBlockingQueue ()
创建 LinkedBlockingQueue ,容量 MAX_VALUE 。
LinkedBlockingQueue (int capacity)
用给定(固定)容量创建一个 LinkedBlockingQueue 。
| 参数(Parameters) | |
|---|---|
capacity |
int: the capacity of this queue |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if capacity is not greater than zero |
LinkedBlockingQueue (Collection<? extends E> c)
创建 LinkedBlockingQueue ,容量为 MAX_VALUE ,最初包含给定集合的元素,并按集合迭代器的遍历顺序添加。
| 参数(Parameters) | |
|---|---|
c |
Collection: the collection of elements to initially contain |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified collection or any of its elements are 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 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| 参数(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自身导致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 ,该方法可能无法仅通过抛出异常来插入元素。
| 参数(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 |
int size ()
返回此队列中的元素数量。
| 返回(Returns) | |
|---|---|
int |
the number of elements in this queue |
Spliterator<E> spliterator ()
通过此队列中的元素返回 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 |