Top Description Inners Fields Constructors Methods
org.apache.commons.pool2.impl

pack-priv final Class LinkedBlockingDeque<E>

extends AbstractQueue<E>
implements BlockingDeque<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.concurrent.BlockingDeque, java.util.Deque, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable, java.util.Queue, java.util.concurrent.BlockingQueue
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.
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.time.Duration, java.util.AbstractQueue, .Collection, .Iterator, .NoSuchElementException, .Objects, java.util.concurrent.BlockingDeque, .TimeUnit, java.util.concurrent.locks.Condition

An optionally-bounded blocking deque based on linked nodes.

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 and Inner Type Summary

Modifier and TypeClass and Description
private abstract class
LinkedBlockingDeque.AbstractItr

Base class for Iterators for LinkedBlockingDeque

private class
private class
LinkedBlockingDeque.Itr

Forward iterator

private static class
LinkedBlockingDeque.Node<
node item type
E
>

Doubly-linked list node class.

Field Summary

Modifier and TypeField and Description
private final int
capacity

Maximum number of items in the deque

private transient int
count

Number of items in the deque

private transient LinkedBlockingDeque.Node<E>
first

Pointer to first node.

private transient LinkedBlockingDeque.Node<E>
last

Pointer to last node.

private final InterruptibleReentrantLock
lock

Main lock guarding all access

private final Condition
notEmpty

Condition for waiting takes

private final Condition
notFull

Condition for waiting puts

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
LinkedBlockingDeque()

Creates a LinkedBlockingDeque with a capacity of Integer#MAX_VALUE.

pack-priv
LinkedBlockingDeque(final boolean
true means threads waiting on the deque should be served as if waiting in a FIFO request queue
fairness
)

Creates a LinkedBlockingDeque with a capacity of Integer#MAX_VALUE and the given fairness policy.

pack-priv
LinkedBlockingDeque(final Collection<? extends E>
the collection of elements to initially contain
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.

pack-priv
LinkedBlockingDeque(final int
the capacity of this deque
capacity
)

Creates a LinkedBlockingDeque with the given (fixed) capacity.

pack-priv
LinkedBlockingDeque(final int
the capacity of this deque
capacity
,
final boolean
true means threads waiting on the deque should be served as if waiting in a FIFO request queue
fairness
)

Creates a LinkedBlockingDeque with the given (fixed) capacity and fairness policy.

Method Summary

Modifier and TypeMethod and Description
public boolean
add(final E e)

Overrides java.util.AbstractQueue.add.

Implements java.util.concurrent.BlockingDeque.add, java.util.Queue.add, java.util.Collection.add.

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) 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.

public void
addFirst(final E e)

Implements java.util.concurrent.BlockingDeque.addFirst.

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

public void
addLast(final E e)

Implements java.util.concurrent.BlockingDeque.addLast.

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

public void
clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Atomically removes all of the elements from this deque.

public boolean

Returns:

true if this deque contains the specified element
contains
(final Object
object to be checked for containment in this deque
o
)

Overrides java.util.AbstractCollection.contains.

Implements java.util.concurrent.BlockingDeque.contains, java.util.Collection.contains.

Returns true if this deque contains the specified element.

public Iterator<E>
descendingIterator()

Implements java.util.Deque.descendingIterator.

Returns an iterator over the elements in this deque in reverse sequential order.

public int

Returns:

number of elements added to the collection
drainTo
(final Collection<? super E>
The collection to add the elements to
c
)

Implements java.util.concurrent.BlockingQueue.drainTo.

Drains the queue to the specified collection.

public int

Returns:

number of elements added to the collection
drainTo
(final Collection<? super E>
collection to add the elements to
collection
,
final int
maximum number of elements to remove from the queue
maxElements
)

Implements java.util.concurrent.BlockingQueue.drainTo.

Drains no more than the specified number of elements from the queue to the specified collection.

public E

Returns:

the head of the queue represented by this deque
element
()

Overrides java.util.AbstractQueue.element.

Implements java.util.concurrent.BlockingDeque.element, java.util.Queue.element.

Retrieves, but does not remove, the head of the queue represented by this deque.

public E
getFirst()

Implements java.util.Deque.getFirst.

Retrieves, but does not remove, the first element of this deque.

public E
getLast()

Implements java.util.Deque.getLast.

Retrieves, but does not remove, the last element of this deque.

pack-priv int

Returns:

number of threads waiting on this deque's notEmpty condition.
getTakeQueueLength
()

Gets the length of the queue of threads waiting to take instances from this deque.

pack-priv boolean

Returns:

true if there is at least one thread waiting on this deque's notEmpty condition.
hasTakeWaiters
()

Returns true if there are threads waiting to take instances from this deque.

pack-priv void
interuptTakeWaiters()

Interrupts the threads currently waiting to take an object from the pool.

public Iterator<E>

Returns:

an iterator over the elements in this deque in proper sequence
iterator
()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.concurrent.BlockingDeque.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this deque in proper sequence.

private boolean

Returns:

true if successful, otherwise false
linkFirst
(final E
The element to link as the first element.
e
)

Links provided element as first element, or returns false if full.

private boolean

Returns:

true if successful, otherwise false
linkLast
(final E
The element to link as the last element.
e
)

Links provided element as last element, or returns false if full.

public boolean
offer(final E e)

Implements java.util.concurrent.BlockingDeque.offer, java.util.Queue.offer.

Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

pack-priv boolean

Returns:

true if successful, otherwise false
offer
(final E
element to link
e
,
final Duration
length of time to wait
timeout
)

Links the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.

public boolean

Returns:

true if successful, otherwise false
offer
(final E
element to link
e
,
final long
length of time to wait
timeout
,
final TimeUnit
units that timeout is expressed in
unit
)

Implements java.util.concurrent.BlockingDeque.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.

public boolean
offerFirst(final E e)

Implements java.util.concurrent.BlockingDeque.offerFirst.

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

pack-priv boolean

Returns:

true if successful, otherwise false
offerFirst
(final E
element to link
e
,
final Duration
length of time to wait
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.

public boolean

Returns:

true if successful, otherwise false
offerFirst
(final E
element to link
e
,
final long
length of time to wait
timeout
,
final TimeUnit
units that timeout is expressed in
unit
)

Implements java.util.concurrent.BlockingDeque.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.

public boolean
offerLast(final E e)

Implements java.util.concurrent.BlockingDeque.offerLast.

Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.

pack-priv boolean

Returns:

true if successful, otherwise false
offerLast
(final E
element to link
e
,
final Duration
length of time to wait
timeout
)

Links the provided element as the last in the queue, waiting up to the specified time to do so if the queue is full.

public boolean

Returns:

true if successful, otherwise false
offerLast
(final E
element to link
e
,
final long
length of time to wait
timeout
,
final TimeUnit
units that timeout is expressed in
unit
)

Implements java.util.concurrent.BlockingDeque.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.

public E
peek()

Implements java.util.concurrent.BlockingDeque.peek, java.util.Queue.peek.

Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

public E
peekFirst()

Implements java.util.Deque.peekFirst.

Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.

public E
peekLast()

Implements java.util.Deque.peekLast.

Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.

public E
poll()

Implements java.util.concurrent.BlockingDeque.poll, java.util.Queue.poll.

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

pack-priv E

Returns:

the unlinked element
poll
(final Duration
length of time to wait
timeout
)

Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.

public E

Returns:

the unlinked element
poll
(final long
length of time to wait
timeout
,
final TimeUnit
units that timeout is expressed in
unit
)

Implements java.util.concurrent.BlockingDeque.poll.

Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.

public E
pollFirst()

Implements java.util.Deque.pollFirst.

Retrieves and removes the first element of this deque, or returns null if this deque is empty.

pack-priv E

Returns:

the unlinked element
pollFirst
(final Duration
length of time to wait
timeout
)

Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.

public E

Returns:

the unlinked element
pollFirst
(final long
length of time to wait
timeout
,
final TimeUnit
units that timeout is expressed in
unit
)

Implements java.util.concurrent.BlockingDeque.pollFirst.

Unlinks the first element in the queue, waiting up to the specified time to do so if the queue is empty.

public E
pollLast()

Implements java.util.Deque.pollLast.

Retrieves and removes the last element of this deque, or returns null if this deque is empty.

pack-priv E

Returns:

the unlinked element
pollLast
(final Duration
length of time to wait
timeout
)

Unlinks the last element in the queue, waiting up to the specified time to do so if the queue is empty.

public E

Returns:

the unlinked element
pollLast
(final long
length of time to wait
timeout
,
final TimeUnit
units that timeout is expressed in
unit
)

Implements java.util.concurrent.BlockingDeque.pollLast.

Unlinks the last element in the queue, waiting up to the specified time to do so if the queue is empty.

public E
pop()

Implements java.util.Deque.pop.

Pops an element from the stack represented by this deque.

public void
push(final E e)

Implements java.util.concurrent.BlockingDeque.push.

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

public void
put(final E
element to link
e
)

Implements java.util.concurrent.BlockingDeque.put.

Links the provided element as the last in the queue, waiting until there is space to do so if the queue is full.

public void
putFirst(final E
element to link
e
)

Implements java.util.concurrent.BlockingDeque.putFirst.

Links the provided element as the first in the queue, waiting until there is space to do so if the queue is full.

public void
putLast(final E
element to link
e
)

Implements java.util.concurrent.BlockingDeque.putLast.

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(final ObjectInputStream
the stream
s
)

Reconstitutes this deque from a stream (that is, deserialize it).

public int

Returns:

The number of additional elements the queue is able to accept
remainingCapacity
()

Implements java.util.concurrent.BlockingQueue.remainingCapacity.

Returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking.

public E

Returns:

the head of the queue represented by this deque
remove
()

Overrides java.util.AbstractQueue.remove.

Implements java.util.concurrent.BlockingDeque.remove, java.util.Queue.remove.

Retrieves and removes the head of the queue represented by this deque.

public boolean

Returns:

true if this deque changed as a result of the call
remove
(final Object
element to be removed from this deque, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.concurrent.BlockingDeque.remove, java.util.Collection.remove.

Removes the first occurrence of the specified element from this deque.

public E
removeFirst()

Implements java.util.Deque.removeFirst.

Retrieves and removes the first element of this deque.

public boolean
removeFirstOccurrence(final Object o)

Implements java.util.concurrent.BlockingDeque.removeFirstOccurrence.

Removes the first occurrence of the specified element from this deque.

public E
removeLast()

Implements java.util.Deque.removeLast.

Retrieves and removes the last element of this deque.

public boolean
removeLastOccurrence(final Object o)

Implements java.util.concurrent.BlockingDeque.removeLastOccurrence.

Removes the last occurrence of the specified element from this deque.

public int

Returns:

the number of elements in this deque
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.concurrent.BlockingDeque.size, java.util.Collection.size.

Returns the number of elements in this deque.

public E

Returns:

the unlinked element
take
()

Implements java.util.concurrent.BlockingDeque.take.

Unlinks the first element in the queue, waiting until there is an element to unlink if the queue is empty.

public E

Returns:

the unlinked element
takeFirst
()

Implements java.util.concurrent.BlockingDeque.takeFirst.

Unlinks the first element in the queue, waiting until there is an element to unlink if the queue is empty.

public E

Returns:

the unlinked element
takeLast
()

Implements java.util.concurrent.BlockingDeque.takeLast.

Unlinks the last element in the queue, waiting until there is an element to unlink if the queue is empty.

public Object[]

Returns:

an array containing all of the elements in this deque
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).

public <T> T[]
toArray(T[] a)

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

public String
toString()

Overrides java.util.AbstractCollection.toString.

Returns a string representation of this collection.

private E

Returns:

The first element or null if empty
unlinkFirst
()

Removes and returns the first element, or null if empty.

private E

Returns:

The first element or null if empty
unlinkLast
()

Removes and returns the last element, or null if empty.

private void
writeObject(final ObjectOutputStream
the stream
s
)

Saves the state of this deque to a stream (that is, serialize it).

Inherited from java.util.AbstractQueue:
addAll