Top Description Inners Fields Constructors Methods
java.util.concurrent

public Class LinkedBlockingDeque<E>

extends AbstractQueue<E>
implements BlockingDeque<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.concurrent.BlockingDeque, java.util.Deque, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable, java.util.Queue, java.util.concurrent.BlockingQueue
Type Parameters
<E>
the type of elements held in this deque
Imports
java.util.AbstractQueue, .Collection, .Iterator, .NoSuchElementException, .Objects, .Spliterator, .Spliterators, java.util.concurrent.locks.Condition, .ReentrantLock, java.util.function.Consumer, .Predicate

An optionally-bounded blocking deque based on linked nodes.

The optional capacity bound constructor argument serves as a way to prevent excessive expansion. The capacity, if unspecified, is equal to Integer#MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity.

Most operations run in constant time (ignoring time spent blocking). Exceptions include remove, removeFirstOccurrence, removeLastOccurrence, contains, iterator.remove(), and the bulk operations, all of which run in linear time.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.

This class is a member of the Java Collections Framework.

Author
Doug Lea
Since
1.6

Nested and Inner Type Summary

Modifier and TypeClass and Description
private abstract class
LinkedBlockingDeque.AbstractItr

Base class for LinkedBlockingDeque iterators.

private class
private class
LinkedBlockingDeque.Itr

Forward iterator

private class
LinkedBlockingDeque.LBDSpliterator

A customized variant of Spliterators.IteratorSpliterator.

pack-priv static class
LinkedBlockingDeque.Node<E>

Doubly-linked list node class

Field Summary

Modifier and TypeField and Description
private final int
capacity

Maximum number of items in the deque

private transient int
count

Number of items in the deque

pack-priv transient LinkedBlockingDeque.Node<E>
first

Pointer to first node.

pack-priv transient LinkedBlockingDeque.Node<E>
last

Pointer to last node.

pack-priv final ReentrantLock
lock

Main lock guarding all access

private final Condition
notEmpty

Condition for waiting takes

private final Condition
notFull

Condition for waiting puts

private static final long

Constructor Summary

AccessConstructor and Description
public
LinkedBlockingDeque()

Creates a LinkedBlockingDeque with a capacity of Integer#MAX_VALUE.

public
LinkedBlockingDeque(int
the capacity of this deque
capacity
)

Creates a LinkedBlockingDeque with the given (fixed) capacity.

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

Creates a LinkedBlockingDeque with a capacity of Integer#MAX_VALUE, 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
add(E
the element to add
e
)

Overrides java.util.AbstractQueue.add.

Implements java.util.concurrent.BlockingDeque.add, java.util.Queue.add, java.util.Collection.add.

Inserts the specified element at the end of this deque unless it would violate capacity restrictions.

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.AbstractQueue.addAll.

Implements java.util.Collection.addAll, java.util.Deque.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.concurrent.BlockingDeque.addFirst.

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

public void
addLast(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.addLast.

Inserts the specified element at the end 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 boolean
bulkRemove(Predicate<? super E> filter)

Implementation of bulk remove methods.

pack-priv void
public void
clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Atomically removes all of the elements from this deque.

public boolean

Returns:

true if this deque contains the specified element
contains
(Object
object to be checked for containment in this deque
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.concurrent.BlockingDeque.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 int
drainTo(Collection<? super E>
the collection to transfer elements into
c
)

Implements java.util.concurrent.BlockingQueue.drainTo.

Removes all available elements from this queue and adds them to the given collection.

public int
drainTo(Collection<? super E>
the collection to transfer elements into
c
,
int
the maximum number of elements to transfer
maxElements
)

Implements java.util.concurrent.BlockingQueue.drainTo.

Removes at most the given number of available elements from this queue and adds them to the given collection.

public E

Returns:

the head of the queue represented by this deque
element
()

Overrides java.util.AbstractQueue.element.

Implements java.util.concurrent.BlockingDeque.element, java.util.Queue.element.

Retrieves, but does not remove, the head of the queue represented by this deque.

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.

pack-priv void
forEachFrom(Consumer<? super E> action, LinkedBlockingDeque.Node<E> p)

Runs action on each element found during a traversal starting at p.

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.

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.concurrent.BlockingDeque.iterator, java.util.Collection.iterator.

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

private boolean
linkFirst(LinkedBlockingDeque.Node<E> node)

Links node as first element, or returns false if full.

private boolean
linkLast(LinkedBlockingDeque.Node<E> node)

Links node as last element, or returns false if full.

public boolean
offer(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.offer, java.util.Queue.offer.

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

public boolean
offer(E
the element to add
e
,
long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingDeque.offer.

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.

public boolean
offerFirst(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.offerFirst.

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

public boolean
offerFirst(E
the element to add
e
,
long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingDeque.offerFirst.

Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.

public boolean
offerLast(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.offerLast.

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

public boolean
offerLast(E
the element to add
e
,
long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingDeque.offerLast.

Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.

public E

Returns:

the head of this deque, or null if this deque is empty
peek
()

Implements java.util.concurrent.BlockingDeque.peek, java.util.Queue.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

Returns:

the head of this deque, or null if this deque is empty
poll
()

Implements java.util.concurrent.BlockingDeque.poll, java.util.Queue.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
poll(long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingDeque.poll.

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.

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
pollFirst(long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingDeque.pollFirst.

Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.

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
pollLast(long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingDeque.pollLast.

Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.

public E
pop()

Implements java.util.Deque.pop.

Pops an element from the stack represented by this deque.

public void
push(E
the element to push
e
)

Implements java.util.concurrent.BlockingDeque.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.

public void
put(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.put.

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.

public void
putFirst(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.putFirst.

Inserts the specified element at the front of this deque, waiting if necessary for space to become available.

public void
putLast(E
the element to add
e
)

Implements java.util.concurrent.BlockingDeque.putLast.

Inserts the specified element at the end of this deque, waiting if necessary for space to become available.

private void
readObject(ObjectInputStream
the stream
s
)

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

public int
remainingCapacity()

Implements java.util.concurrent.BlockingQueue.remainingCapacity.

Returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking.

public E

Returns:

the head of the queue represented by this deque
remove
()

Overrides java.util.AbstractQueue.remove.

Implements java.util.concurrent.BlockingDeque.remove, java.util.Queue.remove.

Retrieves and removes the head of the queue represented by this deque.

public boolean

Returns:

true if this deque changed as a result of the call
remove
(Object
element to be removed from this deque, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.concurrent.BlockingDeque.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
removeFirstOccurrence(Object
element to be removed from this deque, if present
o
)

Implements java.util.concurrent.BlockingDeque.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
removeLastOccurrence(Object
element to be removed from this deque, if present
o
)

Implements java.util.concurrent.BlockingDeque.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).

public int

Returns:

the number of elements in this deque
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.concurrent.BlockingDeque.size, java.util.Collection.size.

Returns the number of elements in this deque.

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 LinkedBlockingDeque.Node<E>
succ(LinkedBlockingDeque.Node<E> p)

Used for any element traversal that is not entirely under lock.

public E
take()

Implements java.util.concurrent.BlockingDeque.take.

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.

public E
takeFirst()

Implements java.util.concurrent.BlockingDeque.takeFirst.

Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.

public E
takeLast()

Implements java.util.concurrent.BlockingDeque.takeLast.

Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.

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; the runtime type of the returned array is that of the specified array.

public String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.

private E
unlinkFirst()

Removes and returns first element, or null if empty.

private E
unlinkLast()

Removes and returns last element, or null if empty.

private void
writeObject(ObjectOutputStream
the stream
s
)

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