Class LinkedBlockingDeque<E>
- Type Parameters:
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.
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,BlockingDeque<E>,BlockingQueue<E>,Deque<E>,Queue<E>
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.
- Since:
- 2.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classBase class for Iterators for LinkedBlockingDequeprivate final classDescending iteratorprivate final classForward iteratorprivate static final classDoubly-linked list node class. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intMaximum number of items in the dequeprivate intNumber of items in the dequeprivate LinkedBlockingDeque.Node<E> Pointer to first node.private LinkedBlockingDeque.Node<E> Pointer to last node.private final InterruptibleReentrantLockMain lock guarding all accessprivate final ConditionCondition for waiting takesprivate final ConditionCondition for waiting putsprivate static final long -
Constructor Summary
ConstructorsConstructorDescriptionCreates aLinkedBlockingDequewith a capacity ofInteger.MAX_VALUE.LinkedBlockingDeque(boolean fairness) Creates aLinkedBlockingDequewith a capacity ofInteger.MAX_VALUEand the given fairness policy.LinkedBlockingDeque(int capacity) Creates aLinkedBlockingDequewith the given (fixed) capacity.LinkedBlockingDeque(int capacity, boolean fairness) Creates aLinkedBlockingDequewith the given (fixed) capacity and fairness policy.LinkedBlockingDeque(Collection<? extends E> c) Creates aLinkedBlockingDequewith a capacity ofInteger.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidvoidvoidclear()Atomically removes all of the elements from this deque.booleanReturnstrueif this deque contains the specified element.intdrainTo(Collection<? super E> c) Drains the queue to the specified collection.intdrainTo(Collection<? super E> collection, int maxElements) Drains no more than the specified number of elements from the queue to the specified collection.element()Retrieves, but does not remove, the head of the queue represented by this deque.getFirst()getLast()(package private) intGets the length of the queue of threads waiting to take instances from this deque.(package private) booleanReturns true if there are threads waiting to take instances from this deque.(package private) voidInterrupts the threads currently waiting to take an object from the pool.iterator()Returns an iterator over the elements in this deque in proper sequence.private booleanLinks provided element as first element, or returns false if full.private booleanLinks provided element as last element, or returns false if full.booleanbooleanLinks the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.(package private) booleanLinks the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.booleanofferFirst(E e) booleanofferFirst(E e, long timeout, 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.(package private) booleanofferFirst(E e, Duration timeout) Links the provided element as the first in the queue, waiting up to the specified time to do so if the queue is full.booleanbooleanLinks the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.(package private) booleanLinks the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.peek()peekLast()poll()Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.(package private) EUnlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.(package private) EUnlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.pollLast()Unlinks the last element in the queue, waiting up to the specified time to do so if the queue is empty.(package private) EUnlinks the last element in the queue, waiting up to the specified time to do so if the queue is empty.pop()voidvoidLinks the provided element as the last in the queue, waiting until there is space to do so if the queue is full.voidLinks the provided element as the first in the queue, waiting until there is space to do so if the queue is full.voidLinks the provided element as the last in the queue, waiting until there is space to do so if the queue is full.private voidReconstitutes this deque from a stream (that is, deserialize it).intReturns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking.remove()Retrieves and removes the head of the queue represented by this deque.booleanRemoves the first occurrence of the specified element from this deque.booleanbooleanintsize()Returns the number of elements in this deque.take()Unlinks the first element in the queue, waiting until there is an element to unlink if the queue is empty.Unlinks the first element in the queue, waiting until there is an element to unlink if the queue is empty.takeLast()Unlinks the last element in the queue, waiting until there is an element to unlink if the queue is empty.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) toString()private voidUnlinks the provided node.private ERemoves and returns the first element, or null if empty.private ERemoves and returns the last element, or null if empty.private voidSaves the state of this deque to a stream (that is, serialize it).Methods inherited from class java.util.AbstractQueue
addAllMethods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAllMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
first
Pointer to first node. Invariant: (first == null invalid input: '&'invalid input: '&' last == null) || (first.prev == null invalid input: '&'invalid input: '&' first.item != null) -
last
Pointer to last node. Invariant: (first == null invalid input: '&'invalid input: '&' last == null) || (last.next == null invalid input: '&'invalid input: '&' last.item != null) -
count
private transient int countNumber of items in the deque -
capacity
private final int capacityMaximum number of items in the deque -
lock
Main lock guarding all access -
notEmpty
Condition for waiting takes -
notFull
Condition for waiting puts
-
-
Constructor Details
-
LinkedBlockingDeque
LinkedBlockingDeque()Creates aLinkedBlockingDequewith a capacity ofInteger.MAX_VALUE. -
LinkedBlockingDeque
LinkedBlockingDeque(boolean fairness) Creates aLinkedBlockingDequewith a capacity ofInteger.MAX_VALUEand the given fairness policy.- Parameters:
fairness- true means threads waiting on the deque should be served as if waiting in a FIFO request queue
-
LinkedBlockingDeque
LinkedBlockingDeque(Collection<? extends E> c) Creates aLinkedBlockingDequewith a capacity ofInteger.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.- Parameters:
c- the collection of elements to initially contain- Throws:
NullPointerException- if the specified collection or any of its elements are null
-
LinkedBlockingDeque
LinkedBlockingDeque(int capacity) Creates aLinkedBlockingDequewith the given (fixed) capacity.- Parameters:
capacity- the capacity of this deque- Throws:
IllegalArgumentException- ifcapacityis less than 1
-
LinkedBlockingDeque
LinkedBlockingDeque(int capacity, boolean fairness) Creates aLinkedBlockingDequewith the given (fixed) capacity and fairness policy.- Parameters:
capacity- the capacity of this dequefairness- true means threads waiting on the deque should be served as if waiting in a FIFO request queue- Throws:
IllegalArgumentException- ifcapacityis less than 1
-
-
Method Details
-
add
- Specified by:
addin interfaceBlockingDeque<E>- Specified by:
addin interfaceBlockingQueue<E>- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceDeque<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractQueue<E>
-
addFirst
-
addLast
-
clear
public void clear()Atomically removes all of the elements from this deque. The deque will be empty after this call returns.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>
-
contains
Returnstrueif this deque contains the specified element. More formally, returnstrueif and only if this deque contains at least one elementesuch thato.equals(e).- Specified by:
containsin interfaceBlockingDeque<E>- Specified by:
containsin interfaceBlockingQueue<E>- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceDeque<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- object to be checked for containment in this deque- Returns:
trueif this deque contains the specified element
-
descendingIterator
- Specified by:
descendingIteratorin interfaceDeque<E>
-
drainTo
Drains the queue to the specified collection.- Specified by:
drainToin interfaceBlockingQueue<E>- Parameters:
c- The collection to add the elements to- Returns:
- number of elements added to the collection
- Throws:
UnsupportedOperationException- if the add operation is not supported by the specified collectionClassCastException- if the class of the elements held by this collection prevents them from being added to the specified collectionNullPointerException- if c is nullIllegalArgumentException- if c is this instance
-
drainTo
Drains no more than the specified number of elements from the queue to the specified collection.- Specified by:
drainToin interfaceBlockingQueue<E>- Parameters:
collection- collection to add the elements tomaxElements- maximum number of elements to remove from the queue- Returns:
- number of elements added to the collection
- Throws:
UnsupportedOperationException- if the add operation is not supported by the specified collectionClassCastException- if the class of the elements held by this collection prevents them from being added to the specified collectionNullPointerException- if c is nullIllegalArgumentException- if c is this instance
-
element
Retrieves, but does not remove, the head of the queue represented by this deque. This method differs frompeekonly in that it throws an exception if this deque is empty.This method is equivalent to
getFirst.- Specified by:
elementin interfaceBlockingDeque<E>- Specified by:
elementin interfaceDeque<E>- Specified by:
elementin interfaceQueue<E>- Overrides:
elementin classAbstractQueue<E>- Returns:
- the head of the queue represented by this deque
- Throws:
NoSuchElementException- if this deque is empty
-
getFirst
-
getLast
-
getTakeQueueLength
int getTakeQueueLength()Gets the length of the queue of threads waiting to take instances from this deque. See disclaimer on accuracy inReentrantLock.getWaitQueueLength(Condition).- Returns:
- number of threads waiting on this deque's notEmpty condition.
-
hasTakeWaiters
boolean hasTakeWaiters()Returns true if there are threads waiting to take instances from this deque. See disclaimer on accuracy inReentrantLock.hasWaiters(Condition).- Returns:
- true if there is at least one thread waiting on this deque's notEmpty condition.
-
interruptTakeWaiters
void interruptTakeWaiters()Interrupts the threads currently waiting to take an object from the pool. See disclaimer on accuracy inReentrantLock.getWaitingThreads(Condition). -
iterator
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). The returnedIteratoris a "weakly consistent" iterator that will never throwConcurrentModificationException, 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.- Specified by:
iteratorin interfaceBlockingDeque<E>- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceDeque<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- an iterator over the elements in this deque in proper sequence
-
linkFirst
Links provided element as first element, or returns false if full.- Parameters:
e- The element to link as the first element.- Returns:
trueif successful, otherwisefalse
-
linkLast
Links provided element as last element, or returns false if full.- Parameters:
e- The element to link as the last element.- Returns:
trueif successful, otherwisefalse
-
offer
-
offer
Links the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.This method is equivalent to
offerLast(Object, long, TimeUnit)- Parameters:
e- element to linktimeout- length of time to wait- Returns:
trueif successful, otherwisefalse- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
offer
Links the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.This method is equivalent to
offerLast(Object, long, TimeUnit)- Specified by:
offerin interfaceBlockingDeque<E>- Specified by:
offerin interfaceBlockingQueue<E>- Parameters:
e- element to linktimeout- length of time to waitunit- units that timeout is expressed in- Returns:
trueif successful, otherwisefalse- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
offerFirst
- Specified by:
offerFirstin interfaceBlockingDeque<E>- Specified by:
offerFirstin interfaceDeque<E>
-
offerFirst
Links the provided element as the first in the queue, waiting up to the specified time to do so if the queue is full.- Parameters:
e- element to linktimeout- length of time to wait- Returns:
trueif successful, otherwisefalse- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
offerFirst
Links the provided element as the first in the queue, waiting up to the specified time to do so if the queue is full.- Specified by:
offerFirstin interfaceBlockingDeque<E>- Parameters:
e- element to linktimeout- length of time to waitunit- units that timeout is expressed in- Returns:
trueif successful, otherwisefalse- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
offerLast
-
offerLast
Links the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.- Parameters:
e- element to linktimeout- length of time to wait- Returns:
trueif successful, otherwisefalse- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whist waiting for space
-
offerLast
Links the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.- Specified by:
offerLastin interfaceBlockingDeque<E>- Parameters:
e- element to linktimeout- length of time to waitunit- units that timeout is expressed in- Returns:
trueif successful, otherwisefalse- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whist waiting for space
-
peek
-
peekFirst
-
peekLast
-
poll
-
poll
Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.This method is equivalent to
pollFirst(long, TimeUnit).- Parameters:
timeout- length of time to wait- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
poll
Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.This method is equivalent to
pollFirst(long, TimeUnit).- Specified by:
pollin interfaceBlockingDeque<E>- Specified by:
pollin interfaceBlockingQueue<E>- Parameters:
timeout- length of time to waitunit- units that timeout is expressed in- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
pollFirst
-
pollFirst
Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.- Parameters:
timeout- length of time to wait- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
pollFirst
Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.- Specified by:
pollFirstin interfaceBlockingDeque<E>- Parameters:
timeout- length of time to waitunit- units that timeout is expressed in- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
pollLast
-
pollLast
Unlinks the last element in the queue, waiting up to the specified time to do so if the queue is empty.- Parameters:
timeout- length of time to wait- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
pollLast
Unlinks the last element in the queue, waiting up to the specified time to do so if the queue is empty.- Specified by:
pollLastin interfaceBlockingDeque<E>- Parameters:
timeout- length of time to waitunit- units that timeout is expressed in- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
pop
-
push
-
put
Links the provided element as the last in the queue, waiting until there is space to do so if the queue is full.This method is equivalent to
putLast(Object).- Specified by:
putin interfaceBlockingDeque<E>- Specified by:
putin interfaceBlockingQueue<E>- Parameters:
e- element to link- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
putFirst
Links the provided element as the first in the queue, waiting until there is space to do so if the queue is full.- Specified by:
putFirstin interfaceBlockingDeque<E>- Parameters:
e- element to link- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
putLast
Links the provided element as the last in the queue, waiting until there is space to do so if the queue is full.- Specified by:
putLastin interfaceBlockingDeque<E>- Parameters:
e- element to link- Throws:
NullPointerException- if e is nullInterruptedException- if the thread is interrupted whilst waiting for space
-
readObject
Reconstitutes this deque from a stream (that is, deserialize it).- Parameters:
s- the stream- Throws:
IOExceptionClassNotFoundException
-
remainingCapacity
public int remainingCapacity()Returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking. This is always equal to the initial capacity of this deque less the currentsizeof this deque.Note that you cannot always tell if an attempt to insert an element will succeed by inspecting
remainingCapacitybecause it may be the case that another thread is about to insert or remove an element.- Specified by:
remainingCapacityin interfaceBlockingQueue<E>- Returns:
- The number of additional elements the queue is able to accept
-
remove
Retrieves and removes the head of the queue represented by this deque. This method differs frompollonly in that it throws an exception if this deque is empty.This method is equivalent to
removeFirst.- Specified by:
removein interfaceBlockingDeque<E>- Specified by:
removein interfaceDeque<E>- Specified by:
removein interfaceQueue<E>- Overrides:
removein classAbstractQueue<E>- Returns:
- the head of the queue represented by this deque
- Throws:
NoSuchElementException- if this deque is empty
-
remove
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 elementesuch thato.equals(e)(if such an element exists). Returnstrueif this deque contained the specified element (or equivalently, if this deque changed as a result of the call).This method is equivalent to
removeFirstOccurrence.- Specified by:
removein interfaceBlockingDeque<E>- Specified by:
removein interfaceBlockingQueue<E>- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceDeque<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- element to be removed from this deque, if present- Returns:
trueif this deque changed as a result of the call
-
removeFirst
- Specified by:
removeFirstin interfaceDeque<E>
-
removeFirstOccurrence
- Specified by:
removeFirstOccurrencein interfaceBlockingDeque<E>- Specified by:
removeFirstOccurrencein interfaceDeque<E>
-
removeLast
- Specified by:
removeLastin interfaceDeque<E>
-
removeLastOccurrence
- Specified by:
removeLastOccurrencein interfaceBlockingDeque<E>- Specified by:
removeLastOccurrencein interfaceDeque<E>
-
size
public int size()Returns the number of elements in this deque.- Specified by:
sizein interfaceBlockingDeque<E>- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceDeque<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this deque
-
take
Unlinks the first element in the queue, waiting until there is an element to unlink if the queue is empty.This method is equivalent to
takeFirst().- Specified by:
takein interfaceBlockingDeque<E>- Specified by:
takein interfaceBlockingQueue<E>- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
takeFirst
Unlinks the first element in the queue, waiting until there is an element to unlink if the queue is empty.- Specified by:
takeFirstin interfaceBlockingDeque<E>- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
takeLast
Unlinks the last element in the queue, waiting until there is an element to unlink if the queue is empty.- Specified by:
takeLastin interfaceBlockingDeque<E>- Returns:
- the unlinked element
- Throws:
InterruptedException- if the current thread is interrupted
-
toArray
Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).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.
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array containing all of the elements in this deque
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toString
- Overrides:
toStringin classAbstractCollection<E>
-
unlink
Unlinks the provided node.- Parameters:
x- The node to unlink
-
unlinkFirst
Removes and returns the first element, or null if empty.- Returns:
- The first element or
nullif empty
-
unlinkLast
Removes and returns the last element, or null if empty.- Returns:
- The first element or
nullif empty
-
writeObject
Saves the state of this deque to a stream (that is, serialize it).- Parameters:
s- the stream- Throws:
IOException- if I/O errors occur while writing to the underlyingOutputStream
-