E
- the type of elements held in this collectionpublic class LinkedBlockingDeque<E> extends AbstractQueue<E> implements BlockingDeque<E>, Serializable
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.
Constructor and Description |
---|
LinkedBlockingDeque()
Creates a LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE . |
LinkedBlockingDeque(Collection<? extends E> 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. |
LinkedBlockingDeque(int capacity)
Creates a LinkedBlockingDeque with the given (fixed)
capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions, returning
true upon success and throwing an IllegalStateException
if no space is currently available.
|
void |
addFirst(E e)
Inserts the specified element to the front of this deque unless it
would violate capacity restrictions.
|
void |
addLast(E e)
Inserts the specified element to the end of this deque unless it would
violate capacity restrictions.
|
void |
clear()
Atomically removes all of the elements from this deque.
|
boolean |
contains(Object o)
Returns true if this queue contains the specified element.
|
int |
drainTo(Collection<? super E> c)
Removes all available elements from this queue and adds them
to the given collection.
|
int |
drainTo(Collection<? super E> c,
int maxElements)
Removes at most the given number of available elements from
this queue and adds them to the given collection.
|
E |
element()
Retrieves, but does not remove, the head of this queue.
|
E |
getFirst()
Retrieves, but does not remove, the first element of this
deque.
|
E |
getLast()
Retrieves, but does not remove, the last element of this
deque.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this deque in proper sequence.
|
boolean |
offer(E e)
Inserts the specified element into the queue represented by this deque
unless it would violate capacity restrictions.
|
boolean |
offer(E o,
long timeout,
TimeUnit unit)
Inserts the specified element as the lest element of this
deque, if possible.
|
boolean |
offerFirst(E o)
Inserts the specified element to the front this deque unless it would
violate capacity restrictions.
|
boolean |
offerFirst(E o,
long timeout,
TimeUnit unit)
Inserts the specified element as the first element of this deque,
waiting if necessary up to the specified wait time for space to
become available.
|
boolean |
offerLast(E o)
Inserts the specified element to the end of this deque unless it would
violate capacity restrictions.
|
boolean |
offerLast(E o,
long timeout,
TimeUnit unit)
Inserts the specified element as the last element of this deque,
waiting if necessary up to the specified wait time for space to
become available.
|
E |
peek()
Retrieves, but does not remove, the head of the queue represented by
this deque, returning null if this deque is empty.
|
E |
peekFirst()
Retrieves, but does not remove, the first element of this deque,
returning null if this deque is empty.
|
E |
peekLast()
Retrieves, but does not remove, the last element of this deque,
returning null if this deque is empty.
|
E |
poll()
Retrieves and removes the head of the queue represented by
this deque, or null if this deque is empty.
|
E |
poll(long timeout,
TimeUnit unit)
Retrieves and removes the first element of this deque, waiting
if necessary up to the specified wait time if no elements are
present on this deque.
|
E |
pollFirst()
Retrieves and removes the first element of this deque, or
null if this deque is empty.
|
E |
pollFirst(long timeout,
TimeUnit unit)
Retrieves and removes the first element of this deque, waiting
if necessary up to the specified wait time if no elements are
present on this deque.
|
E |
pollLast()
Retrieves and removes the last element of this deque, or
null if this deque is empty.
|
E |
pollLast(long timeout,
TimeUnit unit)
Retrieves and removes the last element of this deque, waiting
if necessary up to the specified wait time if no elements are
present on this deque.
|
E |
pop()
Pops an element from the stack represented by this deque.
|
void |
push(E e)
Pushes an element onto the stack represented by this deque.
|
void |
put(E o)
Adds the specified element as the last element of this deque,
waiting if necessary for space to become available.
|
void |
putFirst(E o)
Adds the specified element as the first element of this deque,
waiting if necessary for space to become available.
|
void |
putLast(E o)
Adds the specified element as the last element of this deque,
waiting if necessary for space to become available.
|
int |
remainingCapacity()
Returns the number of elements that this deque can ideally (in
the absence of memory or resource constraints) accept without
blocking.
|
E |
remove()
Retrieves and removes the head of this queue.
|
boolean |
remove(Object o)
Removes a single instance of the specified element from this queue,
if it is present.
|
E |
removeFirst()
Removes and returns the first element of this deque.
|
boolean |
removeFirstOccurrence(Object e)
Removes the first occurrence of the specified element in this
deque.
|
E |
removeLast()
Retrieves and removes the last element of this deque.
|
boolean |
removeLastOccurrence(Object e)
Removes the last occurrence of the specified element in this
deque.
|
int |
size()
Returns the number of elements in this deque.
|
E |
take()
Retrieves and removes the first element of this deque, waiting
if no elements are present on this deque.
|
E |
takeFirst()
Retrieves and removes the first element of this deque, waiting
if no elements are present on this deque.
|
E |
takeLast()
Retrieves and removes the last element of this deque, waiting
if no elements are present on this deque.
|
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
String |
toString() |
addAll
containsAll, isEmpty, removeAll, retainAll
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll
public LinkedBlockingDeque()
Integer.MAX_VALUE
.public LinkedBlockingDeque(int capacity)
capacity
- the capacity of this dequeIllegalArgumentException
- if capacity is less than 1public LinkedBlockingDeque(Collection<? extends E> c)
Integer.MAX_VALUE
, initially containing the elements of the
given collection,
added in traversal order of the collection's iterator.c
- the collection of elements to initially containNullPointerException
- if c or any element within it
is nullpublic boolean offerFirst(E o)
Deque
offerFirst
in interface Deque<E>
o
- the element to insertpublic boolean offerLast(E o)
Deque
public void addFirst(E e)
Deque
public void addLast(E e)
Deque
public E pollFirst()
Deque
public E pollLast()
Deque
public E removeFirst()
Deque
removeFirst
in interface Deque<E>
public E removeLast()
Deque
removeLast
in interface Deque<E>
public E peekFirst()
Deque
public E peekLast()
Deque
public E getFirst()
Deque
public E getLast()
Deque
public void putFirst(E o) throws InterruptedException
BlockingDeque
putFirst
in interface BlockingDeque<E>
o
- the element to addInterruptedException
- if interrupted while waiting.public void putLast(E o) throws InterruptedException
BlockingDeque
putLast
in interface BlockingDeque<E>
o
- the element to addInterruptedException
- if interrupted while waiting.public E takeFirst() throws InterruptedException
BlockingDeque
takeFirst
in interface BlockingDeque<E>
InterruptedException
- if interrupted while waiting.public E takeLast() throws InterruptedException
BlockingDeque
takeLast
in interface BlockingDeque<E>
InterruptedException
- if interrupted while waiting.public boolean offerFirst(E o, long timeout, TimeUnit unit) throws InterruptedException
BlockingDeque
offerFirst
in interface BlockingDeque<E>
o
- the element to addtimeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameterInterruptedException
- if interrupted while waiting.public boolean offerLast(E o, long timeout, TimeUnit unit) throws InterruptedException
BlockingDeque
offerLast
in interface BlockingDeque<E>
o
- the element to addtimeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameterInterruptedException
- if interrupted while waiting.public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException
BlockingDeque
pollFirst
in interface BlockingDeque<E>
timeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameterInterruptedException
- if interrupted while waiting.public E pollLast(long timeout, TimeUnit unit) throws InterruptedException
BlockingDeque
pollLast
in interface BlockingDeque<E>
timeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameterInterruptedException
- if interrupted while waiting.public boolean offer(E e)
Deque
Deque.add(E)
method, which can fail to insert an element only by
throwing an exception.
This method is equivalent to Deque.offerLast(E)
.
public boolean add(E e)
java.util.AbstractQueue
This implementation returns true if offer succeeds, else throws an IllegalStateException.
add
in interface Collection<E>
add
in interface BlockingQueue<E>
add
in interface Queue<E>
add
in interface Deque<E>
add
in class AbstractQueue<E>
e
- the element to addCollection.add(E)
)public void push(E e)
Deque
This method is equivalent to Deque.addFirst(E)
.
public E poll()
Deque
This method is equivalent to Deque.pollFirst()
.
public E remove()
java.util.AbstractQueue
poll
only in that it throws an exception if this
queue is empty.
This implementation returns the result of poll unless the queue is empty.
public E pop()
Deque
This method is equivalent to Deque.removeFirst()
.
public E peek()
Deque
This method is equivalent to Deque.peekFirst()
public E element()
java.util.AbstractQueue
peek
only in that it throws an exception if
this queue is empty.
This implementation returns the result of peek unless the queue is empty.
public boolean remove(Object o)
java.util.concurrent.BlockingQueue
remove
in interface Collection<E>
remove
in interface BlockingQueue<E>
remove
in class AbstractCollection<E>
o
- element to be removed from this queue, if presentpublic void put(E o) throws InterruptedException
BlockingDeque
put
in interface BlockingQueue<E>
put
in interface BlockingDeque<E>
o
- the element to addInterruptedException
- if interrupted while waiting.public E take() throws InterruptedException
BlockingDeque
take
in interface BlockingQueue<E>
take
in interface BlockingDeque<E>
InterruptedException
- if interrupted while waiting.public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException
BlockingDeque
Collection.add(E)
, which can fail to insert an element only by
throwing an exception. This method is equivalent to
offerLastoffer
in interface BlockingQueue<E>
offer
in interface BlockingDeque<E>
o
- the element to add.timeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameterInterruptedException
- if interrupted while waitingpublic E poll(long timeout, TimeUnit unit) throws InterruptedException
BlockingDeque
poll
in interface BlockingQueue<E>
poll
in interface BlockingDeque<E>
timeout
- how long to wait before giving up, in units of
unitunit
- a TimeUnit determining how to interpret the
timeout parameterInterruptedException
- if interrupted while waiting.public int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public int remainingCapacity()
Note that you cannot always tell if an attempt to add an element will succeed by inspecting remainingCapacity because it may be the case that a waiting consumer is ready to take an element out of an otherwise full deque.
remainingCapacity
in interface BlockingQueue<E>
public boolean contains(Object o)
java.util.concurrent.BlockingQueue
contains
in interface Collection<E>
contains
in interface BlockingQueue<E>
contains
in class AbstractCollection<E>
o
- object to be checked for containment in this queuepublic boolean removeFirstOccurrence(Object e)
Deque
removeFirstOccurrence
in interface Deque<E>
e
- element to be removed from this deque, if presentpublic boolean removeLastOccurrence(Object e)
Deque
removeLastOccurrence
in interface Deque<E>
e
- element to be removed from this deque, if presentpublic Object[] toArray()
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
public <T> T[] toArray(T[] a)
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
public String toString()
toString
in class AbstractCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in class AbstractQueue<E>
public int drainTo(Collection<? super E> c)
java.util.concurrent.BlockingQueue
drainTo
in interface BlockingQueue<E>
c
- the collection to transfer elements intopublic int drainTo(Collection<? super E> c, int maxElements)
java.util.concurrent.BlockingQueue
drainTo
in interface BlockingQueue<E>
c
- the collection to transfer elements intomaxElements
- the maximum number of elements to transferpublic Iterator<E> iterator()
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.