Deque
that additionally supports blocking operations that wait
for the deque to become non-empty when retrieving an element, and wait for
space to become available in the deque when storing an element.
BlockingDeque
methods come in four forms, with different ways
of handling operations that cannot be satisfied immediately, but may be
satisfied at some point in the future:
one throws an exception, the second returns a special value (either
null
or false
, depending on the operation), the third
blocks the current thread indefinitely until the operation can succeed,
and the fourth blocks for only a given maximum time limit before giving
up. These methods are summarized in the following table:
First Element (Head) | ||||
---|---|---|---|---|
Throws exception | Special value | Blocks | Times out | |
Insert | addFirst(e) |
offerFirst(e) |
putFirst(e) |
offerFirst(e, time, unit) |
Remove | removeFirst() |
pollFirst() |
takeFirst() |
pollFirst(time, unit) |
Examine | getFirst() |
peekFirst() |
not applicable | not applicable |
Last Element (Tail) | ||||
Throws exception | Special value | Blocks | Times out | |
Insert | addLast(e) |
offerLast(e) |
putLast(e) |
offerLast(e, time, unit) |
Remove | removeLast() |
pollLast() |
takeLast() |
pollLast(time, unit) |
Examine | getLast() |
peekLast() |
not applicable | not applicable |
Like any BlockingQueue
, a BlockingDeque
is thread safe,
does not permit null elements, and may (or may not) be
capacity-constrained.
A BlockingDeque
implementation may be used directly as a FIFO
BlockingQueue
. The methods inherited from the
BlockingQueue
interface are precisely equivalent to
BlockingDeque
methods as indicated in the following table:
BlockingQueue Method |
Equivalent BlockingDeque Method |
|
---|---|---|
Insert | add(e) |
addLast(e) |
offer(e) |
offerLast(e) |
|
put(e) |
putLast(e) |
|
offer(e, time, unit) |
offerLast(e, time, unit) |
|
Remove | remove() |
removeFirst() |
poll() |
pollFirst() |
|
take() |
takeFirst() |
|
poll(time, unit) |
pollFirst(time, unit) |
|
Examine | element() |
getFirst() |
peek() |
peekFirst() |
Memory consistency effects: As with other concurrent
collections, actions in a thread prior to placing an object into a
BlockingDeque
happen-before
actions subsequent to the access or removal of that element from
the BlockingDeque
in another thread.
This interface is a member of the Java Collections Framework.
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
the element to add e)Redeclares java. 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
|
public void | addFirst(E
the element to add e)Redeclares java. Inserts the specified element at the front of this deque if it is
possible to do so immediately without violating capacity restrictions,
throwing an |
public void | addLast(E
the element to add e)Redeclares java. Inserts the specified element at the end of this deque if it is
possible to do so immediately without violating capacity restrictions,
throwing an |
public boolean | Returns: true if this deque contains the specified elementobject to be checked for containment in this deque o)Redeclares java. Returns |
public E | Returns: the head of this dequeRedeclares java. Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque). |
public Iterator | Returns: an iterator over the elements in this deque in proper sequenceRedeclares java. Returns an iterator over the elements in this deque in proper sequence. |
public boolean | offer(E
the element to add e)Redeclares java. 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
|
public boolean | Returns: true if the element was added to this deque, else
false the element to add e, long how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterRedeclares java. 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)Redeclares java. Inserts the specified element at the front of this deque if it is
possible to do so immediately without violating capacity restrictions,
returning |
public boolean | Returns: true if successful, or false if
the specified waiting time elapses before space is availablethe element to add e, long how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterInserts 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)Redeclares java. Inserts the specified element at the end of this deque if it is
possible to do so immediately without violating capacity restrictions,
returning |
public boolean | Returns: true if successful, or false if
the specified waiting time elapses before space is availablethe element to add e, long how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterInserts 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, ornull if this deque is emptyRedeclares java. 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 |
public E | Returns: the head of this deque, ornull if this deque is emptyRedeclares java. Retrieves and removes the head of the queue represented by this deque
(in other words, the first element of this deque), or returns
|
public E | Returns: the head of this deque, ornull if the
specified waiting time elapses before an element is availablehow long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterRedeclares java. 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 | Returns: the head of this deque, ornull if the specified
waiting time elapses before an element is availablehow long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterRetrieves 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 | Returns: the tail of this deque, ornull if the specified
waiting time elapses before an element is availablehow long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterRetrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available. |
public void | push(E
the element to push e)Redeclares java. 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
|
public void | put(E
the element to add e)Redeclares java. 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)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)Inserts the specified element at the end of this deque, waiting if necessary for space to become available. |
public E | Returns: the head of the queue represented by this dequeRedeclares java. 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 this deque changed as a result of the callelement to be removed from this deque, if present o)Redeclares java. Removes the first occurrence of the specified element from this deque. |
public boolean | Returns: true if an element was removed as a result of this callelement to be removed from this deque, if present o)Redeclares java. Removes the first occurrence of the specified element from this deque. |
public boolean | Returns: true if an element was removed as a result of this callelement to be removed from this deque, if present o)Redeclares java. Removes the last occurrence of the specified element from this deque. |
public int | Returns: the number of elements in this dequeRedeclares java. Returns the number of elements in this deque. |
public E | Returns: the head of this dequeRedeclares java. 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 | Returns: the head of this dequeRetrieves and removes the first element of this deque, waiting if necessary until an element becomes available. |
public E | Returns: the tail of this dequeRetrieves and removes the last element of this deque, waiting if necessary until an element becomes available. |
add | back to summary |
---|---|
public boolean add(E e) Redeclares java. 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
This method is equivalent to
|
addFirst | back to summary |
---|---|
public void addFirst(E e) Redeclares java. Inserts the specified element at the front of this deque if it is
possible to do so immediately without violating capacity restrictions,
throwing an
|
addLast | back to summary |
---|---|
public void addLast(E e) Redeclares java. Inserts the specified element at the end of this deque if it is
possible to do so immediately without violating capacity restrictions,
throwing an
|
contains | back to summary |
---|---|
public boolean contains(Object o) Redeclares java. Returns
|
element | back to summary |
---|---|
public E element() Redeclares java. Retrieves, but does not remove, the head of the queue represented by
this deque (in other words, the first element of this deque).
This method differs from This method is equivalent to
|
iterator | back to summary |
---|---|
public Iterator Redeclares java. Returns an iterator over the elements in this deque in proper sequence. The elements will be returned in order from first (head) to last (tail).
|
offer | back to summary |
---|---|
public boolean offer(E e) Redeclares java. 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
This method is equivalent to
|
offer | back to summary |
---|---|
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException Redeclares java. 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. This method is equivalent to
|
offerFirst | back to summary |
---|---|
public boolean offerFirst(E e) Redeclares java. Inserts the specified element at the front of this deque if it is
possible to do so immediately without violating capacity restrictions,
returning
|
offerFirst | back to summary |
---|---|
public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.
|
offerLast | back to summary |
---|---|
public boolean offerLast(E e) Redeclares java. Inserts the specified element at the end of this deque if it is
possible to do so immediately without violating capacity restrictions,
returning
|
offerLast | back to summary |
---|---|
public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.
|
peek | back to summary |
---|---|
public E peek() Redeclares java. 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 This method is equivalent to
|
poll | back to summary |
---|---|
public E poll() Redeclares java. Retrieves and removes the head of the queue represented by this deque
(in other words, the first element of this deque), or returns
This method is equivalent to
|
poll | back to summary |
---|---|
public E poll(long timeout, TimeUnit unit) throws InterruptedException Redeclares java. 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. This method is equivalent to
|
pollFirst | back to summary |
---|---|
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.
|
pollLast | back to summary |
---|---|
public E pollLast(long timeout, TimeUnit unit) throws InterruptedException Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.
|
push | back to summary |
---|---|
public void push(E e) Redeclares java. 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
This method is equivalent to
|
put | back to summary |
---|---|
public void put(E e) throws InterruptedException Redeclares java. 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. This method is equivalent to
|
putFirst | back to summary |
---|---|
public void putFirst(E e) throws InterruptedException Inserts the specified element at the front of this deque, waiting if necessary for space to become available.
|
putLast | back to summary |
---|---|
public void putLast(E e) throws InterruptedException Inserts the specified element at the end of this deque, waiting if necessary for space to become available.
|
remove | back to summary |
---|---|
public E remove() Redeclares java. Retrieves and removes the head of the queue represented by this deque
(in other words, the first element of this deque).
This method differs from This method is equivalent to
|
remove | back to summary |
---|---|
public boolean remove(Object o) Redeclares java. Removes the first occurrence of the specified element from this deque.
If the deque does not contain the element, it is unchanged.
More formally, removes the first element This method is equivalent to
|
removeFirstOccurrence | back to summary |
---|---|
public boolean removeFirstOccurrence(Object o) Redeclares java. Removes the first occurrence of the specified element from this deque.
If the deque does not contain the element, it is unchanged.
More formally, removes the first element
|
removeLastOccurrence | back to summary |
---|---|
public boolean removeLastOccurrence(Object o) Redeclares java. Removes the last occurrence of the specified element from this deque.
If the deque does not contain the element, it is unchanged.
More formally, removes the last element
|
size | back to summary |
---|---|
public int size() Redeclares java. Returns the number of elements in this deque.
|
take | back to summary |
---|---|
public E take() throws InterruptedException Redeclares java. 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. This method is equivalent to
|
takeFirst | back to summary |
---|---|
public E takeFirst() throws InterruptedException Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.
|
takeLast | back to summary |
---|---|
public E takeLast() throws InterruptedException Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.
|