E - the type of elements held in this collection
Note: This was copied from Apache Harmony and modified to suit the needs of
Commons Pool.class LinkedBlockingDeque<E>
extends java.util.AbstractQueue<E>
implements java.util.Deque<E>, java.io.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.
| Modifier and Type | Class and Description |
|---|---|
private class |
LinkedBlockingDeque.AbstractItr
Base class for Iterators for LinkedBlockingDeque
|
private class |
LinkedBlockingDeque.DescendingItr
Descending iterator
|
private class |
LinkedBlockingDeque.Itr
Forward iterator
|
private static class |
LinkedBlockingDeque.Node<E>
Doubly-linked list node class
|
| Modifier and Type | Field and Description |
|---|---|
private int |
capacity
Maximum number of items in the deque
|
private int |
count
Number of items in the deque
|
private LinkedBlockingDeque.Node<E> |
first
Pointer to first node.
|
private LinkedBlockingDeque.Node<E> |
last
Pointer to last node.
|
private InterruptibleReentrantLock |
lock
Main lock guarding all access
|
private java.util.concurrent.locks.Condition |
notEmpty
Condition for waiting takes
|
private java.util.concurrent.locks.Condition |
notFull
Condition for waiting puts
|
private static long |
serialVersionUID |
| Constructor and Description |
|---|
LinkedBlockingDeque()
Creates a
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE. |
LinkedBlockingDeque(boolean fairness)
Creates a
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE and the given fairness policy. |
LinkedBlockingDeque(java.util.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. |
LinkedBlockingDeque(int capacity,
boolean fairness)
Creates a
LinkedBlockingDeque with the given (fixed) capacity
and fairness policy. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e) |
void |
addFirst(E e) |
void |
addLast(E e) |
void |
clear()
Atomically removes all of the elements from this deque.
|
boolean |
contains(java.lang.Object o)
Returns
true if this deque contains the specified element. |
java.util.Iterator<E> |
descendingIterator() |
int |
drainTo(java.util.Collection<? super E> c)
Empty the queue to the specified collection.
|
int |
drainTo(java.util.Collection<? super E> c,
int maxElements)
Empty no more than the specified number of elements from the queue to the
specified collection.
|
E |
element()
Retrieves, but does not remove, the head of the queue represented by
this deque.
|
E |
getFirst() |
E |
getLast() |
int |
getTakeQueueLength()
Returns the length of the queue of threads waiting to take instances from this deque.
|
boolean |
hasTakeWaiters()
Returns true if there are threads waiting to take instances from this deque.
|
void |
interuptTakeWaiters()
Interrupts the threads currently waiting to take an object from the pool.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this deque in proper sequence.
|
private boolean |
linkFirst(E e)
Links provided element as first element, or returns false if full.
|
private boolean |
linkLast(E e)
Links provided element as last element, or returns false if full.
|
boolean |
offer(E e) |
boolean |
offer(E e,
long timeout,
java.util.concurrent.TimeUnit unit)
Links the provided element as the last in the queue, waiting up to the
specified time to do so if the queue is full.
|
boolean |
offerFirst(E e) |
boolean |
offerFirst(E e,
long timeout,
java.util.concurrent.TimeUnit unit)
Links the provided element as the first in the queue, waiting up to the
specified time to do so if the queue is full.
|
boolean |
offerLast(E e) |
boolean |
offerLast(E e,
long timeout,
java.util.concurrent.TimeUnit unit)
Links the provided element as the last in the queue, waiting up to the
specified time to do so if the queue is full.
|
E |
peek() |
E |
peekFirst() |
E |
peekLast() |
E |
poll() |
E |
poll(long timeout,
java.util.concurrent.TimeUnit unit)
Unlinks the first element in the queue, waiting up to the specified time
to do so if the queue is empty.
|
E |
pollFirst() |
E |
pollFirst(long timeout,
java.util.concurrent.TimeUnit unit)
Unlinks the first element in the queue, waiting up to the specified time
to do so if the queue is empty.
|
E |
pollLast() |
E |
pollLast(long timeout,
java.util.concurrent.TimeUnit unit)
Unlinks the last element in the queue, waiting up to the specified time
to do so if the queue is empty.
|
E |
pop() |
void |
push(E e) |
void |
put(E e)
Links the provided element as the last in the queue, waiting until there
is space to do so if the queue is full.
|
void |
putFirst(E e)
Links the provided element as the first in the queue, waiting until there
is space to do so if the queue is full.
|
void |
putLast(E e)
Links the provided element as the last in the queue, waiting until there
is space to do so if the queue is full.
|
private void |
readObject(java.io.ObjectInputStream s)
Reconstitute this deque from a stream (that is,
deserialize it).
|
int |
remainingCapacity()
Returns the number of additional 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(java.lang.Object o)
Removes the first occurrence of the specified element from this deque.
|
E |
removeFirst() |
boolean |
removeFirstOccurrence(java.lang.Object o) |
E |
removeLast() |
boolean |
removeLastOccurrence(java.lang.Object o) |
int |
size()
Returns the number of elements in this deque.
|
E |
take()
Unlinks the first element in the queue, waiting until there is an element
to unlink if the queue is empty.
|
E |
takeFirst()
Unlinks the first element in the queue, waiting until there is an element
to unlink if the queue is empty.
|
E |
takeLast()
Unlinks the last element in the queue, waiting until there is an element
to unlink if the queue is empty.
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this deque, in
proper sequence (from first to last element).
|
<T> T[] |
toArray(T[] a) |
java.lang.String |
toString() |
private void |
unlink(LinkedBlockingDeque.Node<E> x)
Unlinks the provided node.
|
private E |
unlinkFirst()
Removes and returns the first element, or null if empty.
|
private E |
unlinkLast()
Removes and returns the last element, or null if empty.
|
private void |
writeObject(java.io.ObjectOutputStream s)
Save the state of this deque to a stream (that is, serialize it).
|
containsAll, isEmpty, removeAll, retainAllclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitprivate static final long serialVersionUID
private transient LinkedBlockingDeque.Node<E> first
private transient LinkedBlockingDeque.Node<E> last
private transient int count
private final int capacity
private final InterruptibleReentrantLock lock
private final java.util.concurrent.locks.Condition notEmpty
private final java.util.concurrent.locks.Condition notFull
public LinkedBlockingDeque()
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE.public LinkedBlockingDeque(boolean fairness)
LinkedBlockingDeque with a capacity of
Integer.MAX_VALUE and the given fairness policy.fairness - true means threads waiting on the deque should be served
as if waiting in a FIFO request queuepublic LinkedBlockingDeque(int capacity)
LinkedBlockingDeque with the given (fixed) capacity.capacity - the capacity of this dequejava.lang.IllegalArgumentException - if capacity is less than 1public LinkedBlockingDeque(int capacity,
boolean fairness)
LinkedBlockingDeque with the given (fixed) capacity
and fairness policy.capacity - the capacity of this dequefairness - true means threads waiting on the deque should be served
as if waiting in a FIFO request queuejava.lang.IllegalArgumentException - if capacity is less than 1public LinkedBlockingDeque(java.util.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 containjava.lang.NullPointerException - if the specified collection or any
of its elements are nullprivate boolean linkFirst(E e)
e - The element to link as the first element.true if successful, otherwise falseprivate boolean linkLast(E e)
e - The element to link as the last element.true if successful, otherwise falseprivate E unlinkFirst()
null if emptyprivate E unlinkLast()
null if emptyprivate void unlink(LinkedBlockingDeque.Node<E> x)
x - The node to unlinkpublic void putFirst(E e) throws java.lang.InterruptedException
e - element to linkjava.lang.NullPointerExceptionjava.lang.InterruptedExceptionpublic void putLast(E e) throws java.lang.InterruptedException
e - element to linkjava.lang.NullPointerExceptionjava.lang.InterruptedExceptionpublic boolean offerFirst(E e, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
e - element to linktimeout - length of time to waitunit - units that timeout is expressed intrue if successful, otherwise falsejava.lang.NullPointerExceptionjava.lang.InterruptedExceptionpublic boolean offerLast(E e, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
e - element to linktimeout - length of time to waitunit - units that timeout is expressed intrue if successful, otherwise falsejava.lang.NullPointerExceptionjava.lang.InterruptedExceptionpublic E takeFirst() throws java.lang.InterruptedException
java.lang.InterruptedException - if the current thread is interruptedpublic E takeLast() throws java.lang.InterruptedException
java.lang.InterruptedException - if the current thread is interruptedpublic E pollFirst(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
timeout - length of time to waitunit - units that timeout is expressed injava.lang.InterruptedException - if the current thread is interruptedpublic E pollLast(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
timeout - length of time to waitunit - units that timeout is expressed injava.lang.InterruptedException - if the current thread is interruptedpublic boolean removeFirstOccurrence(java.lang.Object o)
removeFirstOccurrence in interface java.util.Deque<E>public boolean removeLastOccurrence(java.lang.Object o)
removeLastOccurrence in interface java.util.Deque<E>public boolean add(E e)
public boolean offer(E e)
public void put(E e) throws java.lang.InterruptedException
This method is equivalent to putLast(Object).
e - element to linkjava.lang.NullPointerExceptionjava.lang.InterruptedExceptionpublic boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
This method is equivalent to offerLast(Object, long, TimeUnit)
e - element to linktimeout - length of time to waitunit - units that timeout is expressed intrue if successful, otherwise falsejava.lang.NullPointerExceptionjava.lang.InterruptedExceptionpublic E remove()
poll only in that it throws an
exception if this deque is empty.
This method is equivalent to removeFirst.
public E poll()
public E take() throws java.lang.InterruptedException
This method is equivalent to takeFirst().
java.lang.InterruptedException - if the current thread is interruptedpublic E poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
This method is equivalent to pollFirst(long, TimeUnit).
timeout - length of time to waitunit - units that timeout is expressed injava.lang.InterruptedException - if the current thread is interruptedpublic E element()
public E peek()
public int remainingCapacity()
size of this deque.
Note that you cannot always tell if an attempt to insert
an element will succeed by inspecting remainingCapacity
because it may be the case that another thread is about to
insert or remove an element.
public int drainTo(java.util.Collection<? super E> c)
c - The collection to add the elements tojava.lang.UnsupportedOperationExceptionjava.lang.ClassCastExceptionjava.lang.NullPointerExceptionjava.lang.IllegalArgumentExceptionpublic int drainTo(java.util.Collection<? super E> c, int maxElements)
c - collection to add the elements tomaxElements - maximum number of elements to remove from the queuejava.lang.UnsupportedOperationExceptionjava.lang.ClassCastExceptionjava.lang.NullPointerExceptionjava.lang.IllegalArgumentExceptionpublic boolean remove(java.lang.Object o)
e such that
o.equals(e) (if such an element exists).
Returns true if this deque contained the specified element
(or equivalently, if this deque changed as a result of the call).
This method is equivalent to
removeFirstOccurrence.
public int size()
public boolean contains(java.lang.Object o)
true if this deque contains the specified element.
More formally, returns true if and only if this deque contains
at least one element e such that o.equals(e).contains in interface java.util.Collection<E>contains in interface java.util.Deque<E>contains in class java.util.AbstractCollection<E>o - object to be checked for containment in this dequetrue if this deque contains the specified elementpublic java.lang.Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this deque. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
public <T> T[] toArray(T[] a)
public java.lang.String toString()
toString in class java.util.AbstractCollection<E>public void clear()
public java.util.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.iterator in interface java.lang.Iterable<E>iterator in interface java.util.Collection<E>iterator in interface java.util.Deque<E>iterator in class java.util.AbstractCollection<E>public java.util.Iterator<E> descendingIterator()
descendingIterator in interface java.util.Deque<E>private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
s - the streamjava.io.IOExceptionprivate void readObject(java.io.ObjectInputStream s)
throws java.io.IOException,
java.lang.ClassNotFoundException
s - the streamjava.io.IOExceptionjava.lang.ClassNotFoundExceptionpublic boolean hasTakeWaiters()
ReentrantLock.hasWaiters(Condition).public int getTakeQueueLength()
ReentrantLock.getWaitQueueLength(Condition).public void interuptTakeWaiters()
ReentrantLock.getWaitingThreads(Condition).