Top Description Inners Fields Constructors Methods
java.util.concurrent

public Class ConcurrentLinkedDeque<E>

extends AbstractCollection<E>
implements Deque<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Deque, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable, java.util.Queue
Type Parameters
<E>
the type of elements held in this deque
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.util.AbstractCollection, .Arrays, .Collection, .Deque, .Iterator, .NoSuchElementException, .Objects, .Queue, .Spliterator, .Spliterators, java.util.function.Consumer, .Predicate

An unbounded concurrent deque based on linked nodes. Concurrent insertion, removal, and access operations execute safely across multiple threads. A ConcurrentLinkedDeque 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.

Iterators and spliterators are weakly consistent.

Beware that, unlike in most collections, the size method is NOT a constant-time operation. Because of the asynchronous nature of these deques, 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 Deque and Iterator interfaces.

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a ConcurrentLinkedDeque happen-before actions subsequent to the access or removal of that element from the ConcurrentLinkedDeque in another thread.

This class is a member of the Java Collections Framework.

Authors
Doug Lea, Martin Buchholz
Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract class
pack-priv class
ConcurrentLinkedDeque.CLDSpliterator

A customized variant of Spliterators.IteratorSpliterator

private class
private class
ConcurrentLinkedDeque.Itr

Forward iterator

pack-priv static class

Field Summary

Modifier and TypeField and Description
private transient volatile ConcurrentLinkedDeque.Node<E>
head

A node from which the first node on list (that is, the unique node p with p.prev == null && p.next != p) can be reached in O(1) time.

private static final VarHandle
private static final int
private static final VarHandle
private static final VarHandle
private static final ConcurrentLinkedDeque.Node<Object>
private static final VarHandle
private static final ConcurrentLinkedDeque.Node<Object>
private static final long
private transient volatile ConcurrentLinkedDeque.Node<E>
tail

A node from which the last node on list (that is, the unique node p with p.next == null && p.prev != p) can be reached in O(1) time.

private static final VarHandle

Constructor Summary

AccessConstructor and Description
public
ConcurrentLinkedDeque()

Constructs an empty deque.

public
ConcurrentLinkedDeque(Collection<? extends E>
the collection of elements to initially contain
c
)

Constructs a deque initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true (as specified by Collection#add)
add
(E
the element to add
e
)

Overrides java.util.AbstractCollection.add.

Implements java.util.Deque.add, java.util.Collection.add.

Inserts the specified element at the tail of this deque.

public boolean

Returns:

true if this deque changed as a result of the call
addAll
(Collection<? extends E>
the elements to be inserted into this deque
c
)

Overrides java.util.AbstractCollection.addAll.

Implements java.util.Deque.addAll, java.util.Collection.addAll.

Appends all of the elements in the specified collection to the end of this deque, in the order that they are returned by the specified collection's iterator.

public void
addFirst(E
the element to add
e
)

Implements java.util.Deque.addFirst.

Inserts the specified element at the front of this deque.

public void
addLast(E
the element to add
e
)

Implements java.util.Deque.addLast.

Inserts the specified element at the end of this deque.

private boolean
bulkRemove(Predicate<? super E> filter)

Implementation of bulk remove methods.

public void
clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this deque.

public boolean

Returns:

true if this deque contains the specified element
contains
(Object
element whose presence in this deque is to be tested
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.Deque.contains, java.util.Collection.contains.

Returns true if this deque contains the specified element.

public Iterator<E>

Returns:

an iterator over the elements in this deque in reverse order
descendingIterator
()

Implements java.util.Deque.descendingIterator.

Returns an iterator over the elements in this deque in reverse sequential order.

public E
element()

Implements java.util.Deque.element.

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).

pack-priv ConcurrentLinkedDeque.Node<E>
first()

Returns the first node, the unique node p for which: p.prev == null && p.next != p The returned node may or may not be logically deleted.

public void
forEach(Consumer<? super E>
The action to be performed for each element
action
)

Overrides default java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

public E
getFirst()

Implements java.util.Deque.getFirst.

Retrieves, but does not remove, the first element of this deque.

public E
getLast()

Implements java.util.Deque.getLast.

Retrieves, but does not remove, the last element of this deque.

private void
initHeadTail(ConcurrentLinkedDeque.Node<E> h, ConcurrentLinkedDeque.Node<E> t)

Initializes head and tail, ensuring invariants hold.

public boolean

Returns:

true if this collection contains no elements
isEmpty
()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this collection contains no elements.

public Iterator<E>

Returns:

an iterator over the elements in this deque in proper sequence
iterator
()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Deque.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this deque in proper sequence.

pack-priv ConcurrentLinkedDeque.Node<E>
last()

Returns the last node, the unique node p for which: p.next == null && p.prev != p The returned node may or may not be logically deleted.

private void
linkFirst(E e)

Links e as first element.

private void
linkLast(E e)

Links e as last element.

pack-priv static <E> ConcurrentLinkedDeque.Node<E>
newNode(E item)

Returns a new node holding item.

pack-priv ConcurrentLinkedDeque.Node<E>
public boolean

Returns:

true (as specified by Queue#offer)
offer
(E
the element to add
e
)

Implements java.util.Deque.offer.

Inserts the specified element at the tail of this deque.

public boolean

Returns:

true (as specified by Deque#offerFirst)
offerFirst
(E
the element to add
e
)

Implements java.util.Deque.offerFirst.

Inserts the specified element at the front of this deque.

public boolean

Returns:

true (as specified by Deque#offerLast)
offerLast
(E
the element to add
e
)

Implements java.util.Deque.offerLast.

Inserts the specified element at the end of this deque.

public E
peek()

Implements java.util.Deque.peek.

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

public E
peekFirst()

Implements java.util.Deque.peekFirst.

Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

public E
peekLast()

Implements java.util.Deque.peekLast.

Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.

public E
poll()

Implements java.util.Deque.poll.

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

public E
pollFirst()

Implements java.util.Deque.pollFirst.

Retrieves and removes the first element of this deque, or returns null if this deque is empty.

public E
pollLast()

Implements java.util.Deque.pollLast.

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

public E
pop()

Implements java.util.Deque.pop.

Pops an element from the stack represented by this deque.

pack-priv final ConcurrentLinkedDeque.Node<E>
pred(ConcurrentLinkedDeque.Node<E> p)

Returns the predecessor of p, or the last node if p.prev has been linked to self, which will only be true if traversing with a stale pointer that is now off the list.

pack-priv ConcurrentLinkedDeque.Node<E>
public void
push(E
the element to push
e
)

Implements java.util.Deque.push.

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

private void
readObject(ObjectInputStream
the stream
s
)

Reconstitutes this deque from a stream (that is, deserializes it).

public E
remove()

Implements java.util.Deque.remove.

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).

public boolean

Returns:

true if the deque contained the specified element
remove
(Object
element to be removed from this deque, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Deque.remove, java.util.Collection.remove.

Removes the first occurrence of the specified element from this deque.

public boolean
removeAll(Collection<?>
collection containing elements to be removed from this collection
c
)

Overrides java.util.AbstractCollection.removeAll.

Implements java.util.Collection.removeAll.

Removes all of this collection's elements that are also contained in the specified collection (optional operation).

public E
removeFirst()

Implements java.util.Deque.removeFirst.

Retrieves and removes the first element of this deque.

public boolean

Returns:

true if the deque contained the specified element
removeFirstOccurrence
(Object
element to be removed from this deque, if present
o
)

Implements java.util.Deque.removeFirstOccurrence.

Removes the first occurrence of the specified element from this deque.

public boolean
removeIf(Predicate<? super E>
a predicate which returns true for elements to be removed
filter
)

Overrides default java.util.Collection.removeIf.

Removes all of the elements of this collection that satisfy the given predicate (optional operation).

public E
removeLast()

Implements java.util.Deque.removeLast.

Retrieves and removes the last element of this deque.

public boolean

Returns:

true if the deque contained the specified element
removeLastOccurrence
(Object
element to be removed from this deque, if present
o
)

Implements java.util.Deque.removeLastOccurrence.

Removes the last occurrence of the specified element from this deque.

public boolean
retainAll(Collection<?>
collection containing elements to be retained in this collection
c
)

Overrides java.util.AbstractCollection.retainAll.

Implements java.util.Collection.retainAll.

Retains only the elements in this collection that are contained in the specified collection (optional operation).

private E

Returns:

the element
screenNullResult
(E
the element
v
)

Returns element unless it is null, in which case throws NoSuchElementException.

public int

Returns:

the number of elements in this deque
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Deque.size, java.util.Collection.size.

Returns the number of elements in this deque.

private void
private void
public Spliterator<E>

Returns:

a Spliterator over the elements in this deque
spliterator
()

Overrides default java.util.Collection.spliterator.

Returns a Spliterator over the elements in this deque.

pack-priv final ConcurrentLinkedDeque.Node<E>
succ(ConcurrentLinkedDeque.Node<E> p)

Returns the successor of p, or the first 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 deque
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).

public <T> T[]

Returns:

an array containing all of the elements in this deque
toArray
(T[]
the array into which the elements of the deque 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.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this deque, in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

private Object[]
public String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.

private void
unlinkFirst(ConcurrentLinkedDeque.Node<E> first, ConcurrentLinkedDeque.Node<E> next)

Unlinks non-null first node.

private void
unlinkLast(ConcurrentLinkedDeque.Node<E> last, ConcurrentLinkedDeque.Node<E> prev)

Unlinks non-null last node.

private final void
updateHead()

Guarantees that any node which was unlinked before a call to this method will be unreachable from head after it returns.

private final void
updateTail()

Guarantees that any node which was unlinked before a call to this method will be unreachable from tail after it returns.

private void
writeObject(ObjectOutputStream
the stream
s
)

Saves this deque to a stream (that is, serializes it).

Inherited from java.util.AbstractCollection:
containsAll