Build 1.0_r1(from source)

java.util.concurrent
Class ConcurrentLinkedQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by java.util.concurrent.ConcurrentLinkedQueue<E>
Type Parameters:
E - the type of elements held in this collection
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>

public class ConcurrentLinkedQueue<E>
extends AbstractQueue<E>
implements Queue<E>, Serializable

An unbounded thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection. This queue does not permit null elements.

This implementation employs an efficient "wait-free" 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.

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.

This class implements all of the optional methods of the Collection and Iterator interfaces.

Since:
1.5
See Also:
Serialized Form

Constructor Summary
ConcurrentLinkedQueue()
          Creates a ConcurrentLinkedQueue that is initially empty.
ConcurrentLinkedQueue(Collection<? extends E> c)
          Creates a ConcurrentLinkedQueue initially containing the elements of the given collection, added in traversal order of the collection's iterator.
 
Method Summary
 boolean add(E o)
          Adds the specified element to the tail of this queue.
 boolean contains(Object o)
          Searches this Collection for the specified object.
(package private)  java.util.concurrent.ConcurrentLinkedQueue.Node<E> first()
          Returns the first actual (non-header) node on list.
 boolean isEmpty()
          Returns true if the collection has no element, otherwise false.
 Iterator<E> iterator()
          Returns an iterator over the elements in this queue in proper sequence.
 boolean offer(E o)
          Inserts the specified element to the tail of this queue.
 E peek()
          Gets but not removes the element in the head of the queue, or throws exception if there is no element in the queue.
 E poll()
          Gets and removes the element in the head of the queue, or returns null if there is no element in the queue.
 boolean remove(Object o)
          Removes the first occurrence of the specified object from this Collection.
 int size()
          Returns the number of elements in this queue.
 Object[] toArray()
          Returns a new array containing all elements contained in this Collection.
<T> T[]
toArray(T[] a)
          Returns an array containing all elements contained in this Collection.
 
Methods inherited from class java.util.AbstractQueue
addAll, clear, element, remove
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Queue
element, remove
 
Methods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, removeAll, retainAll
 

Constructor Detail

ConcurrentLinkedQueue

public ConcurrentLinkedQueue()
Creates a ConcurrentLinkedQueue that is initially empty.


ConcurrentLinkedQueue

public ConcurrentLinkedQueue(Collection<? extends E> c)
Creates a ConcurrentLinkedQueue initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Parameters:
c - the collection of elements to initially contain
Throws:
NullPointerException - if c or any element within it is null
Method Detail

add

public boolean add(E o)
Adds the specified element to the tail of this queue.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractQueue<E>
Parameters:
o - the element to add.
Returns:
true (as per the general contract of Collection.add).
Throws:
NullPointerException - if the specified element is null

offer

public boolean offer(E o)
Inserts the specified element to the tail of this queue.

Specified by:
offer in interface Queue<E>
Parameters:
o - the element to add.
Returns:
true (as per the general contract of Queue.offer).
Throws:
NullPointerException - if the specified element is null

poll

public E poll()
Description copied from interface: Queue
Gets and removes the element in the head of the queue, or returns null if there is no element in the queue.

Specified by:
poll in interface Queue<E>
Returns:
the element in the head of the queue or null if there is no element in the queue.

peek

public E peek()
Description copied from interface: Queue
Gets but not removes the element in the head of the queue, or throws exception if there is no element in the queue.

Specified by:
peek in interface Queue<E>
Returns:
the element in the head of the queue or null if there is no element in the queue.

first

java.util.concurrent.ConcurrentLinkedQueue.Node<E> first()
Returns the first actual (non-header) node on list. This is yet another variant of poll/peek; here returning out the first node, not element (so we cannot collapse with peek() without introducing race.)


isEmpty

public boolean isEmpty()
Description copied from class: AbstractCollection
Returns true if the collection has no element, otherwise false.

Specified by:
isEmpty in interface Collection<E>
Overrides:
isEmpty in class AbstractCollection<E>
Returns:
true if the collection has no element.
See Also:
Collection.size()

size

public int size()
Returns the number of elements in this queue. If this queue contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

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.

Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of elements in this queue.

contains

public boolean contains(Object o)
Description copied from class: AbstractCollection
Searches this Collection for the specified object.

Specified by:
contains in interface Collection<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
o - the object to search for
Returns:
true if object is an element of this Collection, false otherwise

remove

public boolean remove(Object o)
Description copied from class: AbstractCollection
Removes the first occurrence of the specified object from this Collection. This operation traverses over the collection, looking for the specified object. Once the object is found, the object will be removed from the collection using the iterator's remove method. This collection will throw an UnsupportedOperationException if the iterator returned does not implement remove method, and the specified object is in this collection.

Specified by:
remove in interface Collection<E>
Overrides:
remove in class AbstractCollection<E>
Parameters:
o - the object to remove
Returns:
true if this Collection is modified, false otherwise

toArray

public Object[] toArray()
Description copied from class: AbstractCollection
Returns a new array containing all elements contained in this Collection. All the elements in the array will not be referenced by the collection. The elements in the returned array will be sorted to the same order as those returned by the iterator of this collection itself if the collection guarantees the order.

Specified by:
toArray in interface Collection<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array of the elements from this Collection

toArray

public <T> T[] toArray(T[] a)
Description copied from class: AbstractCollection
Returns an array containing all elements contained in this Collection. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than this Collection, the array element following the collection elements is set to null.

Specified by:
toArray in interface Collection<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
a - the array
Returns:
an array of the elements from this Collection

iterator

public Iterator<E> iterator()
Returns an iterator over the elements in this queue in proper sequence. The returned iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:
an iterator over the elements in this queue in proper sequence.
See Also:
Iterator

Build 1.0_r1(from source)

Please submit a feedback, bug or feature