|
FlexDoc/Javadoc 2.0 Demo Java Doc |
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 ConcurrentModificationException, 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(Collection), removeIf(Predicate) or forEach(Consumer), 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.
Constructor Summary |
||
Creates a ConcurrentLinkedQueue that is initially empty.
|
||
Creates a ConcurrentLinkedQueue
initially containing the elements of the given collection,
added in traversal order of the collection's iterator.
|
Method Summary |
||
boolean |
Inserts the specified element at the tail of this queue.
|
|
boolean |
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.
|
|
void |
clear()
Removes all of the elements from this queue.
|
|
boolean |
Returns true if this queue contains the specified element.
|
|
void |
Performs the given action for each element of the Iterable
until all elements have been processed or the action throws an
exception.
|
|
boolean |
isEmpty()
Returns true if this queue contains no elements.
|
|
iterator()
Returns an iterator over the elements in this queue in proper sequence.
|
||
boolean |
Inserts the specified element at the tail of this queue.
|
|
peek()
Retrieves, but does not remove, the head of this queue,
or returns null if this queue is empty.
|
||
poll()
Retrieves and removes the head of this queue,
or returns null if this queue is empty.
|
||
boolean |
Removes a single instance of the specified element from this queue,
if it is present.
|
|
boolean |
removeAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the
specified collection (optional operation).
|
|
boolean |
Removes all of the elements of this collection that satisfy the given
predicate.
|
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the
specified collection (optional operation).
|
|
int |
size()
Returns the number of elements in this queue.
|
|
Returns a Spliterator over the elements in this queue.
|
||
Object[] |
toArray()
Returns an array containing all of the elements in this queue, in
proper sequence.
|
|
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.
|
||
toString()
Returns a string representation of this collection.
|
Methods inherited from class java.util.AbstractQueue |
Methods inherited from class java.util.AbstractCollection |
Methods inherited from class java.lang.Object |
Methods inherited from interface java.util.Collection |
public ConcurrentLinkedQueue |
() |
public ConcurrentLinkedQueue |
(Collection<? extends E> c) |
public boolean add |
(E e) |
public boolean offer |
(E e) |
public E poll |
() |
public E peek |
() |
public boolean isEmpty |
() |
public int size |
() |
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.
public boolean contains |
(Object o) |
public boolean remove |
(Object o) |
public boolean addAll |
(Collection<? extends E> c) |
public String toString |
() |
public Object[] toArray |
() |
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.
(T[] a) |
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 null.
Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().
() |
The returned iterator is weakly consistent.
() |
The returned spliterator is weakly consistent.
The Spliterator reports Spliterator.CONCURRENT, Spliterator.ORDERED, and Spliterator.NONNULL.
public boolean removeIf |
public boolean removeAll |
(Collection<?> c) |
public boolean retainAll |
(Collection<?> c) |
public void clear |
() |
This implementation repeatedly invokes poll until it returns null.
public void forEach |
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.
|
FlexDoc/Javadoc 2.0 Demo Java Doc |