public class LinkedTransferQueue
extends AbstractQueue<E> implements TransferQueue<E>, Serializable
| java.lang.Object | |||
| ↳ | java.util.AbstractCollection<E> | ||
| ↳ | java.util.AbstractQueue<E> | ||
| ↳ | java.util.concurrent.LinkedTransferQueue<E> | ||
基于链接节点的无界TransferQueue 。 该队列针对任何给定的生产者命令元素FIFO(先进先出)。 队列的头部是已经在队列中的某个生产者最长时间的元素。 队列的尾部是队列中某个生产者最短时间的元素。
请注意,与大多数收藏不同, size方法不是一个常量操作。 由于这些队列的异步性质,确定当前元素数量需要遍历元素,因此如果在遍历期间修改此集合,则可能会报告不准确的结果。 此外,该批量操作addAll , removeAll , retainAll , containsAll , equals和toArray待原子方式执行, 不能保证。 例如,与addAll操作同时运行的迭代器可能仅查看一些添加的元素。
该类及其迭代器实现了 Collection和 Iterator接口的所有 可选方法。
内存一致性影响:与其他并发集合一样,在将某个对象放入 LinkedTransferQueue happen-before之后的 LinkedTransferQueue 线程中的操作将在另一个线程中访问或从 LinkedTransferQueue中除去该元素。
Public constructors |
|
|---|---|
LinkedTransferQueue() 创建一个最初为空的 |
|
LinkedTransferQueue(Collection<? extends E> c) 创建一个 |
|
Public methods |
|
|---|---|
boolean |
add(E e) 在此队列的尾部插入指定的元素。 |
boolean |
contains(Object o) 如果此队列包含指定的元素,则返回 |
int |
drainTo(Collection<? super E> c, int maxElements) 最多从此队列中移除给定数量的可用元素,并将它们添加到给定集合中。 |
int |
drainTo(Collection<? super E> c) 从该队列中移除所有可用的元素,并将它们添加到给定的集合中。 |
int |
getWaitingConsumerCount() |
boolean |
hasWaitingConsumer() |
boolean |
isEmpty() 如果此队列不包含元素,则返回 |
Iterator<E> |
iterator() 以正确的顺序返回此队列中元素的迭代器。 |
boolean |
offer(E e, long timeout, TimeUnit unit) 在此队列的尾部插入指定的元素。 |
boolean |
offer(E e) 在此队列的尾部插入指定的元素。 |
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() 检索并删除此队列的头部,如果需要等待,直到元素变为可用。 |
Object[] |
toArray() 以适当的顺序返回包含此队列中所有元素的数组。 |
<T> T[] |
toArray(T[] a) 以适当的顺序返回包含此队列中所有元素的数组; 返回数组的运行时类型是指定数组的运行时类型。 |
String |
toString() 返回此集合的字符串表示形式。 |
void |
transfer(E e) 将元素转移给消费者,如果需要的话等待。 |
boolean |
tryTransfer(E e) 如果可能,立即将元素转移给等待的消费者。 |
boolean |
tryTransfer(E e, long timeout, TimeUnit unit) 如果在超时时间之前可以这样做,则将该元素传输给消费者。 |
Inherited methods |
|
|---|---|
java.util.AbstractQueue
|
|
java.util.AbstractCollection
|
|
java.lang.Object
|
|
java.util.Queue
|
|
java.util.Collection
|
|
java.util.concurrent.TransferQueue
|
|
java.lang.Iterable
|
|
java.util.concurrent.BlockingQueue
|
|
LinkedTransferQueue (Collection<? extends E> c)
创建一个 LinkedTransferQueue最初包含给定集合的元素,并按集合迭代器的遍历顺序添加。
| Parameters | |
|---|---|
c |
Collection: the collection of elements to initially contain |
| Throws | |
|---|---|
NullPointerException |
if the specified collection or any of its elements are null |
boolean add (E e)
在此队列的尾部插入指定的元素。 由于队列无IllegalStateException ,此方法永远不会抛出IllegalStateException或返回false 。
| Parameters | |
|---|---|
e |
E: the element to add |
| Returns | |
|---|---|
boolean |
true (as specified by add(E)) |
| Throws | |
|---|---|
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 。 此外,如果在操作正在进行时修改了指定的集合,则此操作的行为未定义。
| 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 | |
|---|---|
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 | |
|---|---|
NullPointerException |
|
IllegalArgumentException |
|
int getWaitingConsumerCount ()
通过take()或定时poll返回等待接收元素的消费者数量的估计值。 返回值近似于一种事态,如果消费者已经完成或放弃等待,这可能是不准确的。 该值可能对监视和启发式有用,但不适用于同步控制。 这种方法的实现可能明显比hasWaitingConsumer()慢。
| Returns | |
|---|---|
int |
the number of consumers waiting to receive elements |
boolean hasWaitingConsumer ()
返回true ,如果有至少一个消费者等待经由以接收元件take()或定时poll 。 返回值代表了一种瞬间的事态。
| Returns | |
|---|---|
boolean |
true if there is at least one waiting consumer |
boolean isEmpty ()
如果此队列不包含元素,则返回 true 。
| Returns | |
|---|---|
boolean |
true if this queue contains no elements |
Iterator<E> iterator ()
以正确的顺序返回此队列中元素的迭代器。 元素将从第一个(头部)到最后一个(尾部)按顺序返回。
返回的迭代器是 weakly consistent 。
| Returns | |
|---|---|
Iterator<E> |
an iterator over the elements in this queue in proper sequence |
boolean offer (E e,
long timeout,
TimeUnit unit)
在此队列的尾部插入指定的元素。 由于队列无false ,此方法不会阻止或返回false 。
| 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 (as specified by BlockingQueue.offer) |
| Throws | |
|---|---|
NullPointerException |
if the specified element is null |
boolean offer (E e)
在此队列的尾部插入指定的元素。 由于队列无false ,此方法永远不会返回false 。
| Parameters | |
|---|---|
e |
E: the element to add |
| Returns | |
|---|---|
boolean |
true (as specified by offer(E)) |
| Throws | |
|---|---|
NullPointerException |
if the specified element is null |
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 | |
|---|---|
NullPointerException |
if the specified element is null |
int remainingCapacity ()
总是返回 Integer.MAX_VALUE因为 LinkedTransferQueue不受容量限制。
| Returns | |
|---|---|
int |
Integer.MAX_VALUE (as specified by BlockingQueue.remainingCapacity) |
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 ()
返回此队列中的元素数量。 如果此队列包含多于Integer.MAX_VALUE元素,则返回Integer.MAX_VALUE 。
请注意,与大多数集合不同,此方法不是一个常量操作。 由于这些队列的异步特性,确定当前元素数量需要进行O(n)遍历。
| Returns | |
|---|---|
int |
the number of elements in this queue |
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 |
|
Object[] toArray ()
以适当的顺序返回包含此队列中所有元素的数组。
返回的数组将是“安全”的,因为此队列不维护对它的引用。 (换句话说,这个方法必须分配一个新的数组)。 调用者可以自由修改返回的数组。
此方法充当基于数组和基于集合的API之间的桥梁。
| Returns | |
|---|---|
Object[] |
an array containing all of the elements in this queue |
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 |
String toString ()
返回此集合的字符串表示形式。 字符串表示由收集元素的列表组成,它们按其迭代器返回的顺序包含在方括号( "[]" )中。 相邻元素由字符", " (逗号和空格)分隔。 元素被转换为字符串,如valueOf(Object) 。
| Returns | |
|---|---|
String |
a string representation of this collection |
void transfer (E e)
将元素转移给消费者,如果需要的话等待。
更准确地说,如果存在消费者已经等待接收它( take()或定时 poll ),则立即传送指定的元素,否则在该队列的尾部插入指定的元素并等待,直到消费者接收到该元素。
| Parameters | |
|---|---|
e |
E: the element to transfer |
| Throws | |
|---|---|
NullPointerException |
if the specified element is null |
InterruptedException |
|
boolean tryTransfer (E e)
如果可能,立即将元素转移给等待的消费者。
更准确地说,如果存在消费者已经等待接收它( take()或定时 poll ),则立即传送指定的元素,否则返回 false而不 false元素。
| Parameters | |
|---|---|
e |
E: the element to transfer |
| Returns | |
|---|---|
boolean |
true if the element was transferred, else false |
| Throws | |
|---|---|
NullPointerException |
if the specified element is null |
boolean tryTransfer (E e,
long timeout,
TimeUnit unit)
如果在超时时间之前可以这样做,则将该元素传输给消费者。
更确切地说,如果存在消费者已经等待接收它( take()或定时 poll ),则立即传送指定的元素,否则在该队列的尾部插入指定的元素,并等待消费者接收到该元素,返回 false如果指定的等待时间已过,则可以传送元素。
| Parameters | |
|---|---|
e |
E: the element to transfer |
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 completion, in which case the element is not left enqueued |
| Throws | |
|---|---|
NullPointerException |
if the specified element is null |
InterruptedException |
|