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 the queue represented by this
deque unless it would violate capacity restrictions.
|
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) |
int |
drainTo(Collection<? super E> c) |
int |
drainTo(Collection<? super E> c,
int maxElements) |
E |
element()
Retrieves, but does not remove, the head of the queue represented by
this deque.
|
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 the queue represented by this deque.
|
boolean |
remove(Object o) |
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() |
addAllcontainsAll, isEmpty, removeAll, retainAllclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitaddAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAllpublic LinkedBlockingDeque()
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE.public LinkedBlockingDeque(int capacity)
LinkedBlockingDeque with the given (fixed)
capacity.capacity - the capacity of this dequeIllegalArgumentException - if capacity is less than 1public LinkedBlockingDeque(Collection<? extends E> c)
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.c - the collection of elements to initially containNullPointerException - if c or any element within it
is nullpublic boolean offerFirst(E o)
DequeaddFirst, which
can fail to insert an element only by throwing an exception.offerFirst in interface Deque<E>o - the element to inserttrue if it was possible to insert the element,
else falsepublic boolean offerLast(E o)
DequeaddLast which
can fail to insert an element only by throwing an exception.public void addFirst(E e)
Dequepublic void addLast(E e)
Dequepublic E pollFirst()
Dequenull if this deque is empty.public E pollLast()
Dequenull if this deque is empty.public E removeFirst()
DequepollFirst method only in that it throws an
exception if this deque is empty.removeFirst in interface Deque<E>public E removeLast()
DequepollLast method only in that it throws an
exception if this deque is empty.removeLast in interface Deque<E>public E peekFirst()
Dequenull if this deque is empty.public E peekLast()
Dequenull if this deque is empty.public E getFirst()
Dequepeek method only
in that it throws an exception if this deque is empty.public E getLast()
Dequepeek method only
in that it throws an exception if this deque is empty.public void putFirst(E o) throws InterruptedException
BlockingDequeputFirst in interface BlockingDeque<E>o - the element to addInterruptedException - if interrupted while waitingpublic void putLast(E o) throws InterruptedException
BlockingDequeputLast in interface BlockingDeque<E>o - the element to addInterruptedException - if interrupted while waitingpublic E takeFirst() throws InterruptedException
BlockingDequetakeFirst in interface BlockingDeque<E>InterruptedException - if interrupted while waitingpublic E takeLast() throws InterruptedException
BlockingDequetakeLast in interface BlockingDeque<E>InterruptedException - if interrupted while waitingpublic boolean offerFirst(E o, long timeout, TimeUnit unit) throws InterruptedException
BlockingDequeofferFirst 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 parametertrue if successful, or false if
the specified waiting time elapses before space is availableInterruptedException - if interrupted while waitingpublic boolean offerLast(E o, long timeout, TimeUnit unit) throws InterruptedException
BlockingDequeofferLast 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 parametertrue if successful, or false if
the specified waiting time elapses before space is availableInterruptedException - if interrupted while waitingpublic E pollFirst(long timeout, TimeUnit unit) throws InterruptedException
BlockingDequepollFirst in interface BlockingDeque<E>timeout - how long to wait before giving up, in units of
unitunit - a TimeUnit determining how to interpret the
timeout parameternull if the
specified waiting time elapses before an element is presentInterruptedException - if interrupted while waitingpublic E pollLast(long timeout, TimeUnit unit) throws InterruptedException
BlockingDequepollLast in interface BlockingDeque<E>timeout - how long to wait before giving up, in units of
unitunit - a TimeUnit determining how to interpret the
timeout parameternull if the
specified waiting time elapses before an element is presentInterruptedException - if interrupted while waitingpublic boolean offer(E e)
DequeDeque.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)
DequeThis method is equivalent to Deque.addLast(E).
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 inserttrue (as per the spec for Collection.add(E))public void push(E e)
DequeThis method is equivalent to Deque.addFirst(E).
public E poll()
Dequenull if this deque is empty. In other words,
retrieves and removes the first element of this deque, or null
if this deque is empty.
This method is equivalent to Deque.pollFirst().
public E remove()
Dequepoll method only in that it
throws an exception if this deque is empty.
This method is equivalent to Deque.removeFirst().
public E pop()
DequeThis method is equivalent to Deque.removeFirst().
public E peek()
Dequenull if this deque is empty.
This method is equivalent to Deque.peekFirst().
public E element()
Dequepeek method only in
that it throws an exception if this deque is empty.
This method is equivalent to Deque.getFirst().
public boolean remove(Object o)
remove in interface Collection<E>remove in interface BlockingQueue<E>remove in class AbstractCollection<E>public void put(E o) throws InterruptedException
BlockingDequeput in interface BlockingQueue<E>put in interface BlockingDeque<E>o - the element to addInterruptedException - if interrupted while waitingpublic E take() throws InterruptedException
BlockingDequetake in interface BlockingQueue<E>take in interface BlockingDeque<E>InterruptedException - if interrupted while waitingpublic boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException
BlockingDequeoffer is generally preferable to method Collection.add(E), which can fail to insert an element only by
throwing an exception. This method is equivalent to
offerLast.offer in interface BlockingQueue<E>offer in interface BlockingDeque<E>o - the element to addtrue if it was possible to add the element to
this deque, else falseInterruptedExceptionpublic E poll(long timeout, TimeUnit unit) throws InterruptedException
BlockingDequepoll 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 parameternull if the
specified waiting time elapses before an element is presentInterruptedException - if interrupted while waitingpublic int size()
size in interface Collection<E>size in class AbstractCollection<E>public int remainingCapacity()
size of this deque.
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)
contains in interface Collection<E>contains in interface BlockingQueue<E>contains in class AbstractCollection<E>public boolean removeFirstOccurrence(Object e)
Dequee
such that (o==null ? e==null : o.equals(e)) (if
such an element exists).removeFirstOccurrence in interface Deque<E>e - element to be removed from this deque, if presenttrue if the deque contained the specified elementpublic boolean removeLastOccurrence(Object e)
Dequee
such that (o==null ? e==null : o.equals(e)) (if
such an element exists).removeLastOccurrence in interface Deque<E>e - element to be removed from this deque, if presenttrue if the deque contained the specified elementpublic 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)
drainTo in interface BlockingQueue<E>public int drainTo(Collection<? super E> c, int maxElements)
drainTo in interface BlockingQueue<E>public Iterator<E> iterator()
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.