ConcurrentLinkedQueue
is an appropriate choice when
many threads will share access to a common collection.
Like most other concurrent collection implementations, this class
does not permit the use of null
elements.
This implementation employs an efficient non-blocking algorithm based on one described in Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms by Maged M. Michael and Michael L. Scott.
Iterators are weakly consistent, returning elements
reflecting the state of the queue at some point at or since the
creation of the iterator. They do not throw java.
, and may proceed concurrently
with other operations. Elements contained in the queue since the creation
of the iterator will be returned exactly once.
Beware that, unlike in most collections, the size
method
is NOT a constant-time operation. Because of the
asynchronous nature of these queues, determining the current number
of elements requires a traversal of the elements, and so may report
inaccurate results if this collection is modified during traversal.
Bulk operations that add, remove, or examine multiple elements,
such as addAll
, removeIf
or forEach
,
are not guaranteed to be performed atomically.
For example, a forEach
traversal concurrent with an addAll
operation might observe only some of the added elements.
This class and its iterator implement all of the optional
methods of the Queue
and Iterator
interfaces.
Memory consistency effects: As with other concurrent
collections, actions in a thread prior to placing an object into a
ConcurrentLinkedQueue
happen-before
actions subsequent to the access or removal of that element from
the ConcurrentLinkedQueue
in another thread.
This class is a member of the Java Collections Framework.
Modifier and Type | Class and Description |
---|---|
pack-priv class | ConcurrentLinkedQueue.
A customized variant of Spliterators.IteratorSpliterator |
private class | |
pack-priv static class |
Modifier and Type | Field and Description |
---|---|
pack-priv transient volatile ConcurrentLinkedQueue. | head
A node from which the first live (non-deleted) node (if any) can be reached in O(1) time. |
private static final VarHandle | |
pack-priv static final VarHandle | |
private static final int | MAX_HOPS
Tolerate this many consecutive dead nodes before CAS-collapsing. |
pack-priv static final VarHandle | |
private static final long | |
private transient volatile ConcurrentLinkedQueue. | tail
A node from which the last node on list (that is, the unique node with node.next == null) can be reached in O(1) time. |
private static final VarHandle |
Access | Constructor and Description |
---|---|
public | |
public | ConcurrentLinkedQueue(Collection<? extends E>
the collection of elements to initially contain c)Creates a |
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
the element to add e)Overrides java. Implements java. Inserts the specified element at the tail of this queue. |
public boolean | Returns: true if this queue changed as a result of the callthe elements to be inserted into this queue c)Overrides java. Implements java. Appends all of the elements in the specified collection to the end of this queue, in the order that they are returned by the specified collection's iterator. |
private boolean | |
public void | clear()
Overrides java. Implements java. Removes all of the elements from this collection (optional operation). |
public boolean | Returns: true if this queue contains the specified elementobject to be checked for containment in this queue o)Overrides java. Implements java. Returns |
pack-priv ConcurrentLinkedQueue. | |
public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Performs the given action for each element of the |
pack-priv void | forEachFrom(Consumer<? super E> action, ConcurrentLinkedQueue.
Runs action on each element found during a traversal starting at p. |
public boolean | Returns: true if this queue contains no elementsOverrides java. Implements java. Returns |
public Iterator | Returns: an iterator over the elements in this queue in proper sequenceImplements abstract java. Implements java. Returns an iterator over the elements in this queue in proper sequence. |
public boolean | offer(E
the element to add e)Implements java. Inserts the specified element at the tail of this queue. |
public E | peek()
Implements java. Retrieves, but does not remove, the head of this queue,
or returns |
public E | poll()
Implements java. Retrieves and removes the head of this queue,
or returns |
private void | readObject(ObjectInputStream
the stream s)Reconstitutes this queue from a stream (that is, deserializes it). |
public boolean | Returns: true if this queue changed as a result of the callelement to be removed from this queue, if present o)Overrides java. Implements java. Removes a single instance of the specified element from this queue, if it is present. |
public boolean | removeAll(Collection<?>
collection containing elements to be removed from this collection c)Overrides java. Implements java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
public boolean | retainAll(Collection<?>
collection containing elements to be retained in this collection c)Overrides java. Implements java. Retains only the elements in this collection that are contained in the specified collection (optional operation). |
public int | Returns: the number of elements in this queueImplements abstract java. Implements java. Returns the number of elements in this queue. |
private ConcurrentLinkedQueue. | Returns: either old pred or p if pred dead or CAS failedthe last known live node, or null if none pred,the first dead node c,the last dead node p,p.next: the next live node, or null if at end qCollapse dead nodes between pred and q. |
public Spliterator | Returns: aSpliterator over the elements in this queueOverrides default java. Returns a |
pack-priv final ConcurrentLinkedQueue. | succ(ConcurrentLinkedQueue.
Returns the successor of p, or the head node if p.next has been linked to self, which will only be true if traversing with a stale pointer that is now off the list. |
public Object[] | Returns: an array containing all of the elements in this queueOverrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence. |
public <T> T[] | Returns: an array containing all of the elements in this queuethe 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 a)Overrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array. |
private Object[] | |
public String | toString()
Overrides java. Returns a string representation of this collection. |
private boolean | tryCasSuccessor(ConcurrentLinkedQueue.
Tries to CAS pred.next (or head, if pred is null) from c to p. |
pack-priv final void | |
private void |
head | back to summary |
---|---|
pack-priv transient volatile ConcurrentLinkedQueue. A node from which the first live (non-deleted) node (if any) can be reached in O(1) time. Invariants: - all live nodes are reachable from head via succ() - head != null - (tmp = head).next != tmp || tmp != head Non-invariants: - head.item may or may not be null. - it is permitted for tail to lag behind head, that is, for tail to not be reachable from head! |
HEAD | back to summary |
---|---|
private static final VarHandle HEAD |
ITEM | back to summary |
---|---|
pack-priv static final VarHandle ITEM |
MAX_HOPS | back to summary |
---|---|
private static final int MAX_HOPS Tolerate this many consecutive dead nodes before CAS-collapsing. Amortized cost of clear() is (1 + 1/MAX_HOPS) CASes per element. |
NEXT | back to summary |
---|---|
pack-priv static final VarHandle NEXT |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
tail | back to summary |
---|---|
private transient volatile ConcurrentLinkedQueue. A node from which the last node on list (that is, the unique node with node.next == null) can be reached in O(1) time. Invariants: - the last node is always reachable from tail via succ() - tail != null Non-invariants: - tail.item may or may not be null. - it is permitted for tail to lag behind head, that is, for tail to not be reachable from head! - tail.next may or may not be self-linked. |
TAIL | back to summary |
---|---|
private static final VarHandle TAIL |
ConcurrentLinkedQueue | back to summary |
---|---|
public ConcurrentLinkedQueue() Creates a |
ConcurrentLinkedQueue | back to summary |
---|---|
public ConcurrentLinkedQueue(Collection<? extends E> c) Creates a
|
add | back to summary |
---|---|
public boolean add(E e) Overrides java. Implements java. Inserts the specified element at the tail of this queue.
As the queue is unbounded, this method will never throw
|
addAll | back to summary |
---|---|
public boolean addAll(Collection<? extends E> c) Overrides java. Implements java. Appends all of the elements in the specified collection to the end of
this queue, in the order that they are returned by the specified
collection's iterator. Attempts to
|
bulkRemove | back to summary |
---|---|
private boolean bulkRemove(Predicate<? super E> filter) Implementation of bulk remove methods. |
clear | back to summary |
---|---|
public void clear() Overrides java. Implements java. Doc from java. Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns. |
contains | back to summary |
---|---|
public boolean contains(Object o) Overrides java. Implements java. Returns
|
first | back to summary |
---|---|
pack-priv ConcurrentLinkedQueue. Returns the first live (non-deleted) node on list, or null if none. This is yet another variant of poll/peek; here returning the first node, not element. We could make peek() a wrapper around first(), but that would cost an extra volatile read of item, and the need to add a retry loop to deal with the possibility of losing a race to a concurrent poll(). |
forEach | back to summary |
---|---|
public void forEach(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each element of the The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy.
|
forEachFrom | back to summary |
---|---|
pack-priv void forEachFrom(Consumer<? super E> action, ConcurrentLinkedQueue. Runs action on each element found during a traversal starting at p. If p is null, the action is not run. |
isEmpty | back to summary |
---|---|
public boolean isEmpty() Overrides java. Implements java. Returns
|
iterator | back to summary |
---|---|
public Iterator Implements abstract java. Implements java. Returns an iterator over the elements in this queue in proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is weakly consistent.
|
offer | back to summary |
---|---|
public boolean offer(E e) Implements java. Inserts the specified element at the tail of this queue.
As the queue is unbounded, this method will never return
|
peek | back to summary |
---|---|
public E peek() Implements java. Doc from java. Retrieves, but does not remove, the head of this queue,
or returns
|
poll | back to summary |
---|---|
public E poll() Implements java. Doc from java. Retrieves and removes the head of this queue,
or returns
|
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Reconstitutes this queue from a stream (that is, deserializes it).
|
remove | back to summary |
---|---|
public boolean remove(Object o) Overrides java. Implements java. Removes a single instance of the specified element from this queue,
if it is present. More formally, removes an element
|
removeAll | back to summary |
---|---|
public boolean removeAll(Collection<?> c) Overrides java. Implements java. Doc from java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
|
removeIf | back to summary |
---|---|
public boolean removeIf(Predicate<? super E> filter) Overrides default java. Doc from java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.
|
retainAll | back to summary |
---|---|
public boolean retainAll(Collection<?> c) Overrides java. Implements java. Doc from java. Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
|
size | back to summary |
---|---|
public int size() Implements abstract java. Implements java. Returns the number of elements in this queue. If this queue
contains more than Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires an O(n) traversal. Additionally, if elements are added or removed during execution of this method, the returned result may be inaccurate. Thus, this method is typically not very useful in concurrent applications.
|
skipDeadNodes | back to summary |
---|---|
private ConcurrentLinkedQueue. Collapse dead nodes between pred and q.
|
spliterator | back to summary |
---|---|
public Spliterator Overrides default java. Returns a The returned spliterator is weakly consistent. The Implementation Note The
|
succ | back to summary |
---|---|
pack-priv final ConcurrentLinkedQueue. Returns the successor of p, or the head node if p.next has been linked to self, which will only be true if traversing with a stale pointer that is now off the list. |
toArray | back to summary |
---|---|
public Object[] toArray() Overrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence. The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs.
|
toArray | back to summary |
---|---|
public <T> T[] toArray(T[] a) Overrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array. If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue. If this queue fits in the specified array with room to spare
(i.e., the array has more elements than this queue), the element in
the array immediately following the end of the queue is set to
Like the Suppose
Note that toArray(new Object[0]) is identical in function to
toArray() .
|
toArrayInternal | back to summary |
---|---|
private Object[] toArrayInternal(Object[] a) |
toString | back to summary |
---|---|
public String toString() Overrides java. Doc from java. Returns a string representation of this collection. The string
representation consists of a list of the collection's elements in the
order they are returned by its iterator, enclosed in square brackets
(
|
tryCasSuccessor | back to summary |
---|---|
private boolean tryCasSuccessor(ConcurrentLinkedQueue. Tries to CAS pred.next (or head, if pred is null) from c to p. Caller must ensure that we're not unlinking the trailing node. |
updateHead | back to summary |
---|---|
pack-priv final void updateHead(ConcurrentLinkedQueue. Tries to CAS head to p. If successful, repoint old head to itself as sentinel for succ(), below. |
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Saves this queue to a stream (that is, serializes it).
|
Modifier and Type | Field and Description |
---|---|
pack-priv int | |
pack-priv ConcurrentLinkedQueue. | |
pack-priv boolean | |
pack-priv static final int |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public int | characteristics()
Implements java. Returns a set of characteristics of this Spliterator and its elements. |
private ConcurrentLinkedQueue. | |
public long | estimateSize()
Implements java. Returns an estimate of the number of elements that would be
encountered by a |
public void | forEachRemaining(Consumer<? super E>
The action action)Overrides default java. Performs the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception. |
private void | |
public boolean | tryAdvance(Consumer<? super E>
The action whose operation is performed at-most once action)Implements java. If a remaining element exists: performs the given action on it,
returning |
public Spliterator | trySplit()
Implements java. If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator. |
batch | back to summary |
---|---|
pack-priv int batch |
current | back to summary |
---|---|
pack-priv ConcurrentLinkedQueue. |
exhausted | back to summary |
---|---|
pack-priv boolean exhausted |
MAX_BATCH | back to summary |
---|---|
pack-priv static final int MAX_BATCH |
CLQSpliterator | back to summary |
---|---|
pack-priv CLQSpliterator() |
characteristics | back to summary |
---|---|
public int characteristics() Implements java. Doc from java. Returns a set of characteristics of this Spliterator and its
elements. The result is represented as ORed values from If a Spliterator reports an inconsistent set of characteristics (either those returned from a single invocation or across multiple invocations), no guarantees can be made about any computation using this Spliterator.
|
current | back to summary |
---|---|
private ConcurrentLinkedQueue. |
estimateSize | back to summary |
---|---|
public long estimateSize() Implements java. Doc from java. Returns an estimate of the number of elements that would be
encountered by a If this Spliterator is
|
forEachRemaining | back to summary |
---|---|
public void forEachRemaining(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each remaining element, sequentially in
the current thread, until all elements have been processed or the action
throws an exception. If this Spliterator is Subsequent behavior of a spliterator is unspecified if the action throws an exception.
|
setCurrent | back to summary |
---|---|
private void setCurrent(ConcurrentLinkedQueue. |
tryAdvance | back to summary |
---|---|
public boolean tryAdvance(Consumer<? super E> action) Implements java. Doc from java. If a remaining element exists: performs the given action on it,
returning Subsequent behavior of a spliterator is unspecified if the action throws an exception.
|
trySplit | back to summary |
---|---|
public Spliterator Implements java. Doc from java. If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator. If this Spliterator is Unless this Spliterator covers an infinite number of elements,
repeated calls to
This method may return
|
Modifier and Type | Field and Description |
---|---|
private ConcurrentLinkedQueue. | lastRet
Node of the last returned item, to support remove. |
private E | nextItem
nextItem holds on to item fields because once we claim that an element exists in hasNext(), we must return it in the following next() call even if it was in the process of being removed when hasNext() was called. |
private ConcurrentLinkedQueue. | nextNode
Next node to return item for. |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public boolean | |
public E | |
public void | remove()
Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
lastRet | back to summary |
---|---|
private ConcurrentLinkedQueue. Node of the last returned item, to support remove. |
nextItem | back to summary |
---|---|
private E nextItem nextItem holds on to item fields because once we claim that an element exists in hasNext(), we must return it in the following next() call even if it was in the process of being removed when hasNext() was called. |
nextNode | back to summary |
---|---|
private ConcurrentLinkedQueue. Next node to return item for. |
Itr | back to summary |
---|---|
pack-priv Itr() |
hasNext | back to summary |
---|---|
public boolean hasNext() Implements java. Doc from java. Returns
|
next | back to summary |
---|---|
public E next() Implements java. Doc from java. Returns the next element in the iteration.
|
remove | back to summary |
---|---|
public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the |
Modifier and Type | Field and Description |
---|---|
pack-priv volatile E | |
pack-priv volatile ConcurrentLinkedQueue. |
Access | Constructor and Description |
---|---|
pack-priv | |
pack-priv |
Modifier and Type | Method and Description |
---|---|
pack-priv void | |
pack-priv boolean |
item | back to summary |
---|---|
pack-priv volatile E item |
next | back to summary |
---|---|
pack-priv volatile ConcurrentLinkedQueue. |
Node | back to summary |
---|---|
pack-priv Node(E item) Constructs a node holding item. Uses relaxed write because item can only be seen after piggy-backing publication via CAS. |
Node | back to summary |
---|---|
pack-priv Node() Constructs a dead dummy node. |
appendRelaxed | back to summary |
---|---|
pack-priv void appendRelaxed(ConcurrentLinkedQueue. |
casItem | back to summary |
---|---|
pack-priv boolean casItem(E cmp, E val) |