public class ConcurrentLinkedQueue
extends AbstractQueue<E> implements Queue<E>, Serializable
| java.lang.Object | |||
| ↳ | java.util.AbstractCollection<E> | ||
| ↳ | java.util.AbstractQueue<E> | ||
| ↳ | java.util.concurrent.ConcurrentLinkedQueue<E> | ||
基于链接节点的无限线程安全queue 。 该队列命令元素FIFO(先进先出)。 队列头是最长时间在队列中的元素。 队列的尾部是已经在队列上的最短时间的元素。 新元素插入到队列尾部,队列检索操作获取队列头部的元素。 当许多线程共享对一个通用集合的访问时, ConcurrentLinkedQueue是一个合适的选择。 像大多数其他并发收集实现一样,该类不允许使用null元素。
该实现采用了基于Maged M. Michael和Michael L. Scott在 Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms中描述的有效 的非阻塞算法。
迭代器是弱一致的 ,在创建迭代器时或之后返回反映队列状态的元素。 他们不扔ConcurrentModificationException ,并可能与其他操作同时进行。 自创建迭代器以来队列中包含的元素将只返回一次。
请注意,与大多数收藏不同, size方法不是一个常量操作。 由于这些队列的异步性质,确定当前元素数量需要遍历元素,因此如果在遍历期间修改此集合,则可能会报告不准确的结果。 此外,该批量操作addAll , removeAll , retainAll , containsAll , equals和toArray待原子方式执行, 不能保证。 例如,与addAll操作同时运行的迭代器可能仅查看一些添加的元素。
该类及其迭代器实现 Queue和 Iterator接口的所有 可选方法。
存储器一致性效果:当与其他并发集合,事先将物体放置成在一个线程动作 ConcurrentLinkedQueue happen-before到该元素的从访问或移除后续动作 ConcurrentLinkedQueue在另一个线程。
Public constructors |
|
|---|---|
ConcurrentLinkedQueue() 创建最初为空的 |
|
ConcurrentLinkedQueue(Collection<? extends E> c) 创建一个 |
|
Public methods |
|
|---|---|
boolean |
add(E e) 在此队列的尾部插入指定的元素。 |
boolean |
addAll(Collection<? extends E> c) 按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到此队列的末尾。 |
boolean |
contains(Object o) 如果此队列包含指定的元素,则返回 |
boolean |
isEmpty() 如果此队列不包含元素,则返回 |
Iterator<E> |
iterator() 以正确的顺序返回此队列中元素的迭代器。 |
boolean |
offer(E e) 在此队列的尾部插入指定的元素。 |
E |
peek() 检索但不移除此队列的头部,或者如果此队列为空,则返回 |
E |
poll() 如果此队列为空,则检索并移除此队列的头部,或返回 |
boolean |
remove(Object o) 从该队列中移除指定元素的单个实例(如果存在)。 |
int |
size() 返回此队列中的元素数量。 |
Spliterator<E> |
spliterator() 在此队列中的元素上返回一个 |
<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.lang.Iterable
|
|
ConcurrentLinkedQueue (Collection<? extends E> c)
创建一个 ConcurrentLinkedQueue最初包含给定集合的元素,并按集合迭代器的遍历顺序添加。
| 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 addAll (Collection<? extends E> c)
按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到此队列的末尾。 试图将addAll排队到自己导致IllegalArgumentException 。
| Parameters | |
|---|---|
c |
Collection: the elements to be inserted into this queue |
| Returns | |
|---|---|
boolean |
true if this queue changed as a result of the call |
| Throws | |
|---|---|
NullPointerException |
if the specified collection or any of its elements are null |
IllegalArgumentException |
if the collection is this queue |
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 |
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)
在此队列的尾部插入指定的元素。 由于队列是无界的,这个方法永远不会返回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 ()
检索并删除此队列的头部,或者如果此队列为空,则返回 null 。
| Returns | |
|---|---|
E |
the head of this queue, or null if this queue is empty |
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 |
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 |