The optional capacity bound constructor argument serves as a
way to prevent excessive queue expansion. The capacity, if unspecified,
is equal to Integer#MAX_VALUE
. Linked nodes are
dynamically created upon each insertion unless this would bring the
queue above capacity.
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 | LinkedBlockingQueue.
Weakly-consistent iterator. |
private class | LinkedBlockingQueue.
A customized variant of Spliterators.IteratorSpliterator. |
pack-priv static class |
Modifier and Type | Field and Description |
---|---|
private final int | capacity
The capacity bound, or Integer.MAX_VALUE if none |
private final AtomicInteger | count
Current number of elements |
pack-priv transient LinkedBlockingQueue. | head
Head of linked list. |
private transient LinkedBlockingQueue. | last
Tail of linked list. |
private final Condition | notEmpty
Wait queue for waiting takes |
private final Condition | notFull
Wait queue for waiting puts |
private final ReentrantLock | putLock
Lock held by put, offer, etc |
private static final long | |
private final ReentrantLock | takeLock
Lock held by take, poll, etc |
Access | Constructor and Description |
---|---|
public | |
public | LinkedBlockingQueue(int
the capacity of this queue capacity)Creates a |
public | LinkedBlockingQueue(Collection<? extends E>
the collection of elements to initially contain c)Creates a |
Modifier and Type | Method and Description |
---|---|
private boolean | |
public void | clear()
Overrides java. Implements java. Atomically removes all of the elements from this queue. |
public boolean | Returns: true if this queue contains the specified elementobject to be checked for containment in this queue o)Overrides java. Implements java. Returns |
private E | |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c)Implements java. Removes all available elements from this queue and adds them to the given collection. |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c, int the maximum number of elements to transfer maxElements)Implements java. Removes at most the given number of available elements from this queue and adds them to the given collection. |
private void | |
pack-priv LinkedBlockingQueue. | findPred(LinkedBlockingQueue.
Returns the predecessor of live node p, given a node that was once a live ancestor of p (or head); allows unlinking of p. |
public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Performs the given action for each element of the |
pack-priv void | forEachFrom(Consumer<? super E> action, LinkedBlockingQueue.
Runs action on each element found during a traversal starting at p. |
pack-priv void | |
pack-priv void | |
public Iterator | Returns: an iterator over the elements in this queue in proper sequenceImplements abstract java. Implements java. Returns an iterator over the elements in this queue in proper sequence. |
public boolean | Returns: true if successful, or false if
the specified waiting time elapses before space is availablethe element to add e, long how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterImplements java. Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available. |
public boolean | offer(E
the element to add e)Implements java. Inserts the specified element at the tail of this queue if it is
possible to do so immediately without exceeding the queue's capacity,
returning |
public E | peek()
Implements java. Retrieves, but does not remove, the head of this queue,
or returns |
public E | poll(long
how long to wait before giving up, in units of
timeout, TimeUnit unit a unit)TimeUnit determining how to interpret the
timeout parameterImplements java. Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available. |
public E | poll()
Implements java. Retrieves and removes the head of this queue,
or returns |
public void | put(E
the element to add e)Implements java. Inserts the specified element at the tail of this queue, waiting if necessary for space to become available. |
private void | readObject(ObjectInputStream
the stream s)Reconstitutes this queue from a stream (that is, deserializes it). |
public int | remainingCapacity()
Implements java. Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking. |
public boolean | Returns: true if this queue changed as a result of the callelement to be removed from this queue, if present o)Overrides java. Implements java. Removes a single instance of the specified element from this queue, if it is present. |
public boolean | removeAll(Collection<?>
collection containing elements to be removed from this collection c)Overrides java. Implements java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). |
public boolean | retainAll(Collection<?>
collection containing elements to be retained in this collection c)Overrides java. Implements java. Retains only the elements in this collection that are contained in the specified collection (optional operation). |
private void | |
private void | |
public int | Returns: the number of elements in this queueImplements abstract java. Implements java. Returns the number of elements in this queue. |
public Spliterator | Returns: aSpliterator over the elements in this queueOverrides default java. Returns a |
pack-priv LinkedBlockingQueue. | |
public E | take()
Implements java. Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
public Object[] | Returns: an array containing all of the elements in this queueOverrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence. |
public <T> T[] | Returns: an array containing all of the elements in this queuethe array into which the elements of the queue are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose a)Overrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array. |
public String | toString()
Overrides java. Returns a string representation of this collection. |
pack-priv void | unlink(LinkedBlockingQueue.
Unlinks interior Node p with predecessor pred. |
private void |
capacity | back to summary |
---|---|
private final int capacity The capacity bound, or Integer.MAX_VALUE if none |
count | back to summary |
---|---|
private final AtomicInteger count Current number of elements |
head | back to summary |
---|---|
pack-priv transient LinkedBlockingQueue. Head of linked list. Invariant: head.item == null |
last | back to summary |
---|---|
private transient LinkedBlockingQueue. Tail of linked list. Invariant: last.next == null |
notEmpty | back to summary |
---|---|
private final Condition notEmpty Wait queue for waiting takes
|
notFull | back to summary |
---|---|
private final Condition notFull Wait queue for waiting puts
|
putLock | back to summary |
---|---|
private final ReentrantLock putLock Lock held by put, offer, etc |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
takeLock | back to summary |
---|---|
private final ReentrantLock takeLock Lock held by take, poll, etc |
LinkedBlockingQueue | back to summary |
---|---|
public LinkedBlockingQueue() Creates a |
LinkedBlockingQueue | back to summary |
---|---|
public LinkedBlockingQueue(int capacity) Creates a
|
LinkedBlockingQueue | back to summary |
---|---|
public LinkedBlockingQueue(Collection<? extends E> c) Creates a
|
bulkRemove | back to summary |
---|---|
private boolean bulkRemove(Predicate<? super E> filter) Implementation of bulk remove methods.
|
clear | back to summary |
---|---|
public void clear() Overrides java. Implements java. Atomically removes all of the elements from this queue. The queue will be empty after this call returns. |
contains | back to summary |
---|---|
public boolean contains(Object o) Overrides java. Implements java. Returns
|
dequeue | back to summary |
---|---|
private E dequeue() Removes a node from head of queue.
|
drainTo | back to summary |
---|---|
public int drainTo(Collection<? super E> c) Implements java. Doc from java. Removes all available elements from this queue and adds them
to the given collection. This operation may be more
efficient than repeatedly polling this queue. A failure
encountered while attempting to add elements to
collection
|
drainTo | back to summary |
---|---|
public int drainTo(Collection<? super E> c, int maxElements) Implements java. Doc from java. Removes at most the given number of available elements from
this queue and adds them to the given collection. A failure
encountered while attempting to add elements to
collection
|
enqueue | back to summary |
---|---|
private void enqueue(LinkedBlockingQueue. Links node at end of queue.
|
findPred | back to summary |
---|---|
pack-priv LinkedBlockingQueue. Returns the predecessor of live node p, given a node that was once a live ancestor of p (or head); allows unlinking of p. |
forEach | back to summary |
---|---|
public void forEach(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each element of the The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy.
|
forEachFrom | back to summary |
---|---|
pack-priv void forEachFrom(Consumer<? super E> action, LinkedBlockingQueue. Runs action on each element found during a traversal starting at p. If p is null, traversal starts at head. |
fullyLock | back to summary |
---|---|
pack-priv void fullyLock() Locks to prevent both puts and takes. |
fullyUnlock | back to summary |
---|---|
pack-priv void fullyUnlock() Unlocks to allow both puts and takes. |
iterator | back to summary |
---|---|
public Iterator Implements abstract java. Implements java. Returns an iterator over the elements in this queue in proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is weakly consistent.
|
offer | back to summary |
---|---|
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException Implements java. Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.
|
offer | back to summary |
---|---|
public boolean offer(E e) Implements java. Inserts the specified element at the tail of this queue if it is
possible to do so immediately without exceeding the queue's capacity,
returning
|
peek | back to summary |
---|---|
public E peek() Implements java. Doc from java. Retrieves, but does not remove, the head of this queue,
or returns
|
poll | back to summary |
---|---|
public E poll(long timeout, TimeUnit unit) throws InterruptedException Implements java. Doc from java. Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
|
poll | back to summary |
---|---|
public E poll() Implements java. Doc from java. Retrieves and removes the head of this queue,
or returns
|
put | back to summary |
---|---|
public void put(E e) throws InterruptedException Implements java. Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.
|
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Reconstitutes this queue from a stream (that is, deserializes it).
|
remainingCapacity | back to summary |
---|---|
public int remainingCapacity() Implements java. Returns the number of additional elements that this queue can ideally
(in the absence of memory or resource constraints) accept without
blocking. This is always equal to the initial capacity of this queue
less the current Note that you cannot always tell if an attempt to insert
an element will succeed by inspecting
|
remove | back to summary |
---|---|
public boolean remove(Object o) Overrides java. Implements java. Removes a single instance of the specified element from this queue,
if it is present. More formally, removes an element
|
removeAll | back to summary |
---|---|
public boolean removeAll(Collection<?> c) Overrides java. Implements java. Doc from java. Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.
|
removeIf | back to summary |
---|---|
public boolean removeIf(Predicate<? super E> filter) Overrides default java. Doc from java. Removes all of the elements of this collection that satisfy the given predicate (optional operation). Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.
|
retainAll | back to summary |
---|---|
public boolean retainAll(Collection<?> c) Overrides java. Implements java. Doc from java. Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.
|
signalNotEmpty | back to summary |
---|---|
private void signalNotEmpty() Signals a waiting take. Called only from put/offer (which do not otherwise ordinarily lock takeLock.) |
signalNotFull | back to summary |
---|---|
private void signalNotFull() Signals a waiting put. Called only from take/poll. |
size | back to summary |
---|---|
public int size() Implements abstract java. Implements java. Returns the number of elements in this queue.
|
spliterator | back to summary |
---|---|
public Spliterator Overrides default java. Returns a The returned spliterator is weakly consistent. The Implementation Note The
|
succ | back to summary |
---|---|
pack-priv LinkedBlockingQueue. Used for any element traversal that is not entirely under lock. Such traversals must handle both: - dequeued nodes (p.next == p) - (possibly multiple) interior removed nodes (p.item == null) |
take | back to summary |
---|---|
public E take() throws InterruptedException Implements java. Doc from java. Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
|
toArray | back to summary |
---|---|
public Object[] toArray() Overrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence. The returned array will be "safe" in that no references to it are maintained by this queue. (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.
|
toArray | back to summary |
---|---|
public <T> T[] toArray(T[] a) Overrides java. Implements java. Returns an array containing all of the elements in this queue, in proper sequence; the runtime type of the returned array is that of the specified array. If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue. If this queue fits in the specified array with room to spare
(i.e., the array has more elements than this queue), the element in
the array immediately following the end of the queue is set to
Like the Suppose
Note that toArray(new Object[0]) is identical in function to
toArray() .
|
toString | back to summary |
---|---|
public String toString() Overrides java. Doc from java. Returns a string representation of this collection. The string
representation consists of a list of the collection's elements in the
order they are returned by its iterator, enclosed in square brackets
(
|
unlink | back to summary |
---|---|
pack-priv void unlink(LinkedBlockingQueue. Unlinks interior Node p with predecessor pred. |
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Saves this queue to a stream (that is, serializes it).
|
Modifier and Type | Field and Description |
---|---|
private LinkedBlockingQueue. | |
private LinkedBlockingQueue. | |
private LinkedBlockingQueue. | |
private E |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public void | forEachRemaining(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Performs the given action for each remaining element until all elements have been processed or the action throws an exception. |
public boolean | |
public E | |
public void | remove()
Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
ancestor | back to summary |
---|---|
private LinkedBlockingQueue. |
lastRet | back to summary |
---|---|
private LinkedBlockingQueue. |
next | back to summary |
---|---|
private LinkedBlockingQueue. |
nextItem | back to summary |
---|---|
private E nextItem |
Itr | back to summary |
---|---|
pack-priv Itr() |
forEachRemaining | back to summary |
---|---|
public void forEachRemaining(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.
The behavior of an iterator is unspecified if the action modifies the
collection in any way (even by calling the Subsequent behavior of an iterator is unspecified if the action throws an exception.
|
hasNext | back to summary |
---|---|
public boolean hasNext() Implements java. Doc from java. Returns
|
next | back to summary |
---|---|
public E next() Implements java. Doc from java. Returns the next element in the iteration.
|
remove | back to summary |
---|---|
public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the |
Modifier and Type | Field and Description |
---|---|
pack-priv int | |
pack-priv LinkedBlockingQueue. | |
pack-priv long | |
pack-priv boolean | |
pack-priv static final int |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public int | characteristics()
Implements java. Returns a set of characteristics of this Spliterator and its elements. |
public long | estimateSize()
Implements java. Returns an estimate of the number of elements that would be
encountered by a |
public void | forEachRemaining(Consumer<? super E>
The action action)Overrides default java. Performs the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception. |
public boolean | tryAdvance(Consumer<? super E>
The action whose operation is performed at-most once action)Implements java. If a remaining element exists: performs the given action on it,
returning |
public Spliterator | trySplit()
Implements java. If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator. |
batch | back to summary |
---|---|
pack-priv int batch |
current | back to summary |
---|---|
pack-priv LinkedBlockingQueue. |
est | back to summary |
---|---|
pack-priv long est |
exhausted | back to summary |
---|---|
pack-priv boolean exhausted |
MAX_BATCH | back to summary |
---|---|
pack-priv static final int MAX_BATCH |
LBQSpliterator | back to summary |
---|---|
pack-priv LBQSpliterator() |
characteristics | back to summary |
---|---|
public int characteristics() Implements java. Doc from java. Returns a set of characteristics of this Spliterator and its
elements. The result is represented as ORed values from If a Spliterator reports an inconsistent set of characteristics (either those returned from a single invocation or across multiple invocations), no guarantees can be made about any computation using this Spliterator.
|
estimateSize | back to summary |
---|---|
public long estimateSize() Implements java. Doc from java. Returns an estimate of the number of elements that would be
encountered by a If this Spliterator is
|
forEachRemaining | back to summary |
---|---|
public void forEachRemaining(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each remaining element, sequentially in
the current thread, until all elements have been processed or the action
throws an exception. If this Spliterator is Subsequent behavior of a spliterator is unspecified if the action throws an exception.
|
tryAdvance | back to summary |
---|---|
public boolean tryAdvance(Consumer<? super E> action) Implements java. Doc from java. If a remaining element exists: performs the given action on it,
returning Subsequent behavior of a spliterator is unspecified if the action throws an exception.
|
trySplit | back to summary |
---|---|
public Spliterator Implements java. Doc from java. If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator. If this Spliterator is Unless this Spliterator covers an infinite number of elements,
repeated calls to
This method may return
|
Modifier and Type | Field and Description |
---|---|
pack-priv E | |
pack-priv LinkedBlockingQueue. | next
One of: - the real successor Node - this Node, meaning the successor is head.next - null, meaning there is no successor (this is the last node) |
Access | Constructor and Description |
---|---|
pack-priv |
item | back to summary |
---|---|
pack-priv E item |
next | back to summary |
---|---|
pack-priv LinkedBlockingQueue. One of: - the real successor Node - this Node, meaning the successor is head.next - null, meaning there is no successor (this is the last node) |
Node | back to summary |
---|---|
pack-priv Node(E x) |