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() |
addAllcontainsAll, isEmpty, removeAll, retainAllclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitaddAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAllpublic 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)
DequeofferFirst in interface Deque<E>o - the element to insertpublic boolean offerLast(E o)
Dequepublic void addFirst(E e)
Dequepublic void addLast(E e)
Dequepublic E pollFirst()
Dequepublic E pollLast()
Dequepublic E removeFirst()
DequeremoveFirst in interface Deque<E>public E removeLast()
DequeremoveLast in interface Deque<E>public E peekFirst()
Dequepublic E peekLast()
Dequepublic E getFirst()
Dequepublic E getLast()
Dequepublic void putFirst(E o) throws InterruptedException
BlockingDequeputFirst in interface BlockingDeque<E>o - the element to addInterruptedException - if interrupted while waiting.public void putLast(E o) throws InterruptedException
BlockingDequeputLast in interface BlockingDeque<E>o - the element to addInterruptedException - if interrupted while waiting.public E takeFirst() throws InterruptedException
BlockingDequetakeFirst in interface BlockingDeque<E>InterruptedException - if interrupted while waiting.public E takeLast() throws InterruptedException
BlockingDequetakeLast in interface BlockingDeque<E>InterruptedException - if interrupted while waiting.public 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 parameterInterruptedException - if interrupted while waiting.public 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 parameterInterruptedException - if interrupted while waiting.public 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 parameterInterruptedException - if interrupted while waiting.public 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 parameterInterruptedException - if interrupted while waiting.public 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)
java.util.AbstractQueueThis 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)
DequeThis method is equivalent to Deque.addFirst(E).
public E poll()
DequeThis method is equivalent to Deque.pollFirst().
public E remove()
java.util.AbstractQueuepoll 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()
DequeThis method is equivalent to Deque.removeFirst().
public E peek()
DequeThis method is equivalent to Deque.peekFirst()
public E element()
java.util.AbstractQueuepeek 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.BlockingQueueremove 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
BlockingDequeput in interface BlockingQueue<E>put in interface BlockingDeque<E>o - the element to addInterruptedException - if interrupted while waiting.public E take() throws InterruptedException
BlockingDequetake 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
BlockingDequeCollection.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
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 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.BlockingQueuecontains 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)
DequeremoveFirstOccurrence in interface Deque<E>e - element to be removed from this deque, if presentpublic boolean removeLastOccurrence(Object e)
DequeremoveLastOccurrence 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.BlockingQueuedrainTo in interface BlockingQueue<E>c - the collection to transfer elements intopublic int drainTo(Collection<? super E> c, int maxElements)
java.util.concurrent.BlockingQueuedrainTo 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.