Top Description Inners Fields Constructors Methods
java.util.concurrent

public Class LinkedBlockingQueue<E>

extends AbstractQueue<E>
implements BlockingQueue<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.concurrent.BlockingQueue, java.util.Queue, java.util.Collection, java.lang.Iterable
Type Parameters
<E>
the type of elements held in this queue
Imports
java.util.AbstractQueue, .Collection, .Iterator, .NoSuchElementException, .Objects, .Spliterator, .Spliterators, java.util.concurrent.atomic.AtomicInteger, java.util.concurrent.locks.Condition, .ReentrantLock, java.util.function.Consumer, .Predicate

An optionally-bounded blocking queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.

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.

Author
Doug Lea
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
LinkedBlockingQueue.Itr

Weakly-consistent iterator.

private class
LinkedBlockingQueue.LBQSpliterator

A customized variant of Spliterators.IteratorSpliterator.

pack-priv static class
LinkedBlockingQueue.Node<E>

Linked list node class.

Field Summary

Modifier and TypeField 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.Node<E>
head

Head of linked list.

private transient LinkedBlockingQueue.Node<E>
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

Constructor Summary

AccessConstructor and Description
public
LinkedBlockingQueue()

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE.

public
LinkedBlockingQueue(int
the capacity of this queue
capacity
)

Creates a LinkedBlockingQueue with the given (fixed) capacity.

public
LinkedBlockingQueue(Collection<? extends E>
the collection of elements to initially contain
c
)

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Method Summary

Modifier and TypeMethod and Description
private boolean
bulkRemove(Predicate<? super E> filter)

Implementation of bulk remove methods.

public void
clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Atomically removes all of the elements from this queue.

public boolean

Returns:

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

Overrides java.util.AbstractCollection.contains.

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

Returns true if this queue contains the specified element.

private E

Returns:

the node
dequeue
()

Removes a node from head of queue.

public int
drainTo(Collection<? super E>
the collection to transfer elements into
c
)

Implements java.util.concurrent.BlockingQueue.drainTo.

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.util.concurrent.BlockingQueue.drainTo.

Removes at most the given number of available elements from this queue and adds them to the given collection.

private void
enqueue(LinkedBlockingQueue.Node<E>
the node
node
)

Links node at end of queue.

pack-priv LinkedBlockingQueue.Node<E>
findPred(LinkedBlockingQueue.Node<E> p, LinkedBlockingQueue.Node<E> ancestor)

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.lang.Iterable.forEach.

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

pack-priv void
forEachFrom(Consumer<? super E> action, LinkedBlockingQueue.Node<E> p)

Runs action on each element found during a traversal starting at p.

pack-priv void
fullyLock()

Locks to prevent both puts and takes.

pack-priv void
fullyUnlock()

Unlocks to allow both puts and takes.

public Iterator<E>

Returns:

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

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

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 available
offer
(E
the element to add
e
,
long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingQueue.offer.

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.util.concurrent.BlockingQueue.offer, java.util.Queue.offer.

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 true upon success and false if this queue is full.

public E
peek()

Implements java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

public E
poll(long
how long to wait before giving up, in units of unit
timeout
,
TimeUnit
a TimeUnit determining how to interpret the timeout parameter
unit
)

Implements java.util.concurrent.BlockingQueue.poll.

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.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.

public void
put(E
the element to add
e
)

Implements java.util.concurrent.BlockingQueue.put.

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.util.concurrent.BlockingQueue.remainingCapacity.

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 call
remove
(Object
element to be removed from this queue, if present
o
)

Overrides java.util.AbstractCollection.remove.

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

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.util.AbstractCollection.removeAll.

Implements java.util.Collection.removeAll.

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 true for elements to be removed
filter
)

Overrides default java.util.Collection.removeIf.

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.util.AbstractCollection.retainAll.

Implements java.util.Collection.retainAll.

Retains only the elements in this collection that are contained in the specified collection (optional operation).

private void
signalNotEmpty()

Signals a waiting take.

private void
signalNotFull()

Signals a waiting put.

public int

Returns:

the number of elements in this queue
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements in this queue.

public Spliterator<E>

Returns:

a Spliterator over the elements in this queue
spliterator
()

Overrides default java.util.Collection.spliterator.

Returns a Spliterator over the elements in this queue.

pack-priv LinkedBlockingQueue.Node<E>
succ(LinkedBlockingQueue.Node<E> p)

Used for any element traversal that is not entirely under lock.

public E
take()

Implements java.util.concurrent.BlockingQueue.take.

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 queue
toArray
()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

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 queue
toArray
(T[]
the 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.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

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.util.AbstractCollection.toString.

Returns a string representation of this collection.

private void
writeObject(ObjectOutputStream
the stream
s
)

Saves this queue to a stream (that is, serializes it).

Inherited from java.util.AbstractQueue:
addaddAllelementremove

Field Detail

capacityback to summary
private final int capacity

The capacity bound, or Integer.MAX_VALUE if none

countback to summary
private final AtomicInteger count

Current number of elements

lastback to summary
private transient LinkedBlockingQueue.Node<E> last

Tail of linked list. Invariant: last.next == null

notEmptyback to summary
private final Condition notEmpty

Wait queue for waiting takes

Annotations
@SuppressWarnings:serial
notFullback to summary
private final Condition notFull

Wait queue for waiting puts

Annotations
@SuppressWarnings:serial
putLockback to summary
private final ReentrantLock putLock

Lock held by put, offer, etc

serialVersionUIDback to summary
private static final long serialVersionUID
takeLockback to summary
private final ReentrantLock takeLock

Lock held by take, poll, etc

Constructor Detail

LinkedBlockingQueueback to summary
public LinkedBlockingQueue()

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE.

LinkedBlockingQueueback to summary
public LinkedBlockingQueue(int capacity)

Creates a LinkedBlockingQueue with the given (fixed) capacity.

Parameters
capacity:int

the capacity of this queue

Exceptions
IllegalArgumentException:
if capacity is not greater than zero
LinkedBlockingQueueback to summary
public LinkedBlockingQueue(Collection<? extends E> c)

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Parameters
c:Collection<? extends E>

the collection of elements to initially contain

Exceptions
NullPointerException:
if the specified collection or any of its elements are null

Method Detail

bulkRemoveback to summary
private boolean bulkRemove(Predicate<? super E> filter)

Implementation of bulk remove methods.

Annotations
@SuppressWarnings:unchecked
clearback to summary
public void clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Atomically removes all of the elements from this queue. The queue will be empty after this call returns.

containsback to summary
public boolean contains(Object o)

Overrides java.util.AbstractCollection.contains.

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

Returns true if this queue contains the specified element. More formally, returns true if and only if this queue contains at least one element e such that o.equals(e).

Parameters
o:Object

object to be checked for containment in this queue

Returns:boolean

true if this queue contains the specified element

dequeueback to summary
private E dequeue()

Removes a node from head of queue.

Returns:E

the node

drainToback to summary
public int drainTo(Collection<? super E> c)

Implements java.util.concurrent.BlockingQueue.drainTo.

Doc from java.util.concurrent.BlockingQueue.drainTo.

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 c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Parameters
c:Collection<? super E>

the collection to transfer elements into

Returns:int

the number of elements transferred

Exceptions
UnsupportedOperationException:
if addition of elements is not supported by the specified collection
ClassCastException:
if the class of an element of this queue prevents it from being added to the specified collection
NullPointerException:
if the specified collection is null
IllegalArgumentException:
if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
drainToback to summary
public int drainTo(Collection<? super E> c, int maxElements)

Implements java.util.concurrent.BlockingQueue.drainTo.

Doc from java.util.concurrent.BlockingQueue.drainTo.

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 c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Parameters
c:Collection<? super E>

the collection to transfer elements into

maxElements:int

the maximum number of elements to transfer

Returns:int

the number of elements transferred

Exceptions
UnsupportedOperationException:
if addition of elements is not supported by the specified collection
ClassCastException:
if the class of an element of this queue prevents it from being added to the specified collection
NullPointerException:
if the specified collection is null
IllegalArgumentException:
if the specified collection is this queue, or some property of an element of this queue prevents it from being added to the specified collection
enqueueback to summary
private void enqueue(LinkedBlockingQueue.Node<E> node)

Links node at end of queue.

Parameters
node:LinkedBlockingQueue.Node<E>

the node

findPredback to summary
pack-priv LinkedBlockingQueue.Node<E> findPred(LinkedBlockingQueue.Node<E> p, LinkedBlockingQueue.Node<E> ancestor)

Returns the predecessor of live node p, given a node that was once a live ancestor of p (or head); allows unlinking of p.

forEachback to summary
public void forEach(Consumer<? super E> action)

Overrides default java.lang.Iterable.forEach.

Doc from java.lang.Iterable.forEach.

Performs the given action for each element of the Iterable 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 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.

Parameters
action:Consumer<? super E>

The action to be performed for each element

Exceptions
NullPointerException:
if the specified action is null
forEachFromback to summary
pack-priv void forEachFrom(Consumer<? super E> action, LinkedBlockingQueue.Node<E> p)

Runs action on each element found during a traversal starting at p. If p is null, traversal starts at head.

fullyLockback to summary
pack-priv void fullyLock()

Locks to prevent both puts and takes.

fullyUnlockback to summary
pack-priv void fullyUnlock()

Unlocks to allow both puts and takes.

iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

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.

Returns:Iterator<E>

an iterator over the elements in this queue in proper sequence

offerback to summary
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.BlockingQueue.offer.

Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.

Parameters
e:E

Doc from java.util.concurrent.BlockingQueue.offer.

the element to add

timeout:long

Doc from java.util.concurrent.BlockingQueue.offer.

how long to wait before giving up, in units of unit

unit:TimeUnit

Doc from java.util.concurrent.BlockingQueue.offer.

a TimeUnit determining how to interpret the timeout parameter

Returns:boolean

true if successful, or false if the specified waiting time elapses before space is available

Exceptions
InterruptedException:
if interrupted while waiting
NullPointerException:
if the specified element is null
offerback to summary
public boolean offer(E e)

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

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 true upon success and false if this queue is full. When using a capacity-restricted queue, this method is generally preferable to method add, which can fail to insert an element only by throwing an exception.

Parameters
e:E

Doc from java.util.concurrent.BlockingQueue.offer.

the element to add

Returns:boolean

Doc from java.util.concurrent.BlockingQueue.offer.

true if the element was added to this queue, else false

Exceptions
NullPointerException:
if the specified element is null
peekback to summary
public E peek()

Implements java.util.Queue.peek.

Doc from java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

pollback to summary
public E poll(long timeout, TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.BlockingQueue.poll.

Doc from java.util.concurrent.BlockingQueue.poll.

Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.

Parameters
timeout:long

how long to wait before giving up, in units of unit

unit:TimeUnit

a TimeUnit determining how to interpret the timeout parameter

Returns:E

the head of this queue, or null if the specified waiting time elapses before an element is available

Exceptions
InterruptedException:
if interrupted while waiting
pollback to summary
public E poll()

Implements java.util.Queue.poll.

Doc from java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

putback to summary
public void put(E e) throws InterruptedException

Implements java.util.concurrent.BlockingQueue.put.

Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.

Parameters
e:E

Doc from java.util.concurrent.BlockingQueue.put.

the element to add

Exceptions
InterruptedException:
if interrupted while waiting
NullPointerException:
if the specified element is null
readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException

Reconstitutes this queue from a stream (that is, deserializes it).

Parameters
s:ObjectInputStream

the stream

Exceptions
IOException:
if an I/O error occurs
ClassNotFoundException:
if the class of a serialized object could not be found
remainingCapacityback to summary
public int remainingCapacity()

Implements java.util.concurrent.BlockingQueue.remainingCapacity.

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 size of this queue.

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.

Returns:int

Doc from java.util.concurrent.BlockingQueue.remainingCapacity.

the remaining capacity

removeback to summary
public boolean remove(Object o)

Overrides java.util.AbstractCollection.remove.

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

Removes a single instance of the specified element from this queue, if it is present. More formally, removes an element e such that o.equals(e), if this queue contains one or more such elements. Returns true if this queue contained the specified element (or equivalently, if this queue changed as a result of the call).

Parameters
o:Object

element to be removed from this queue, if present

Returns:boolean

true if this queue changed as a result of the call

removeAllback to summary
public boolean removeAll(Collection<?> c)

Overrides java.util.AbstractCollection.removeAll.

Implements java.util.Collection.removeAll.

Doc from java.util.Collection.removeAll.

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.

Parameters
c:Collection<?>

collection containing elements to be removed from this collection

Returns:boolean

true if this collection changed as a result of the call

Exceptions
NullPointerException:
if this collection contains one or more null elements and the specified collection does not support null elements (optional) or if the specified collection is null
removeIfback to summary
public boolean removeIf(Predicate<? super E> filter)

Overrides default java.util.Collection.removeIf.

Doc from java.util.Collection.removeIf.

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.

Parameters
filter:Predicate<? super E>

a predicate which returns true for elements to be removed

Returns:boolean

true if any elements were removed

Exceptions
NullPointerException:
if the specified filter is null
retainAllback to summary
public boolean retainAll(Collection<?> c)

Overrides java.util.AbstractCollection.retainAll.

Implements java.util.Collection.retainAll.

Doc from java.util.Collection.retainAll.

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.

Parameters
c:Collection<?>

collection containing elements to be retained in this collection

Returns:boolean

true if this collection changed as a result of the call

Exceptions
NullPointerException:
if this collection contains one or more null elements and the specified collection does not permit null elements (optional) or if the specified collection is null
signalNotEmptyback to summary
private void signalNotEmpty()

Signals a waiting take. Called only from put/offer (which do not otherwise ordinarily lock takeLock.)

signalNotFullback to summary
private void signalNotFull()

Signals a waiting put. Called only from take/poll.

sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements in this queue.

Returns:int

the number of elements in this queue

spliteratorback to summary
public Spliterator<E> spliterator()

Overrides default java.util.Collection.spliterator.

Returns a Spliterator over the elements in this queue.

The returned spliterator is weakly consistent.

The Spliterator reports Spliterator#CONCURRENT, Spliterator#ORDERED, and Spliterator#NONNULL.

Implementation Note

The Spliterator implements trySplit to permit limited parallelism.

Returns:Spliterator<E>

a Spliterator over the elements in this queue

Since
1.8
succback to summary
pack-priv LinkedBlockingQueue.Node<E> succ(LinkedBlockingQueue.Node<E> p)

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)

takeback to summary
public E take() throws InterruptedException

Implements java.util.concurrent.BlockingQueue.take.

Doc from java.util.concurrent.BlockingQueue.take.

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

Returns:E

the head of this queue

Exceptions
InterruptedException:
if interrupted while waiting
toArrayback to summary
public Object[] toArray()

Overrides java.util.AbstractCollection.toArray.

Implements java.util.Collection.toArray.

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.

Returns:Object[]

an array containing all of the elements in this queue

toArrayback to summary
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 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 null.

Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:

 String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
Parameters
<T>

Doc from java.util.Collection.toArray. the component type of the array to contain the collection

a:T[]

the 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

Returns:T[]

an array containing all of the elements in this queue

Annotations
@SuppressWarnings:unchecked
Exceptions
ArrayStoreException:
if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue
NullPointerException:
if the specified array is null
toStringback to summary
public String toString()

Overrides java.util.AbstractCollection.toString.

Doc from java.util.AbstractCollection.toString.

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 ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String#valueOf(Object).

Returns:String

a string representation of this collection

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Saves this queue to a stream (that is, serializes it).

Parameters
s:ObjectOutputStream

the stream

Exceptions
IOException:
if an I/O error occurs
Serial data
The capacity is emitted (int), followed by all of its elements (each an Object) in the proper order, followed by a null
java.util.concurrent back to summary

private Class LinkedBlockingQueue.Itr

extends Object
implements Iterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Weakly-consistent iterator. Lazily updated ancestor field provides expected O(1) remove(), but still O(n) in the worst case, whenever the saved ancestor is concurrently deleted.

Field Summary

Modifier and TypeField and Description
private LinkedBlockingQueue.Node<E>
private LinkedBlockingQueue.Node<E>
private LinkedBlockingQueue.Node<E>
private E

Constructor Summary

AccessConstructor and Description
pack-priv
Itr()

Method Summary

Modifier and TypeMethod and Description
public void
forEachRemaining(Consumer<? super E>
The action to be performed for each element
action
)

Overrides default java.util.Iterator.forEachRemaining.

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public E
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ancestorback to summary
private LinkedBlockingQueue.Node<E> ancestor
lastRetback to summary
private LinkedBlockingQueue.Node<E> lastRet
nextback to summary
private LinkedBlockingQueue.Node<E> next
nextItemback to summary
private E nextItem

Constructor Detail

Itrback to summary
pack-priv Itr()

Method Detail

forEachRemainingback to summary
public void forEachRemaining(Consumer<? super E> action)

Overrides default java.util.Iterator.forEachRemaining.

Doc from java.util.Iterator.forEachRemaining.

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 remove method or other mutator methods of Iterator subtypes), unless an overriding class has specified a concurrent modification policy.

Subsequent behavior of an iterator is unspecified if the action throws an exception.

Parameters
action:Consumer<? super E>

The action to be performed for each element

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

nextback to summary
public E next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:E

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

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 forEachRemaining method.

java.util.concurrent back to summary

private final Class LinkedBlockingQueue.LBQSpliterator

extends Object
implements Spliterator<E>
Class Inheritance
All Implemented Interfaces
java.util.Spliterator

A customized variant of Spliterators.IteratorSpliterator. Keep this class in sync with (very similar) LBDSpliterator.

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv LinkedBlockingQueue.Node<E>
pack-priv long
pack-priv boolean
pack-priv static final int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int
characteristics()

Implements java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements.

public long
estimateSize()

Implements java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

public void
forEachRemaining(Consumer<? super E>
The action
action
)

Overrides default java.util.Spliterator.forEachRemaining.

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.util.Spliterator.tryAdvance.

If a remaining element exists: performs the given action on it, returning true; else returns false.

public Spliterator<E>
trySplit()

Implements java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

batchback to summary
pack-priv int batch
currentback to summary
pack-priv LinkedBlockingQueue.Node<E> current
estback to summary
pack-priv long est
exhaustedback to summary
pack-priv boolean exhausted
MAX_BATCHback to summary
pack-priv static final int MAX_BATCH

Constructor Detail

LBQSpliteratorback to summary
pack-priv LBQSpliterator()

Method Detail

characteristicsback to summary
public int characteristics()

Implements java.util.Spliterator.characteristics.

Doc from java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements. The result is represented as ORed values from ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, SUBSIZED. Repeated calls to characteristics() on a given spliterator, prior to or in-between calls to trySplit, should always return the same result.

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.

Returns:int

a representation of characteristics

estimateSizeback to summary
public long estimateSize()

Implements java.util.Spliterator.estimateSize.

Doc from java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

If this Spliterator is SIZED and has not yet been partially traversed or split, or this Spliterator is SUBSIZED and has not yet been partially traversed, this estimate must be an accurate count of elements that would be encountered by a complete traversal. Otherwise, this estimate may be arbitrarily inaccurate, but must decrease as specified across invocations of trySplit.

Returns:long

the estimated size, or Long.MAX_VALUE if infinite, unknown, or too expensive to compute.

forEachRemainingback to summary
public void forEachRemaining(Consumer<? super E> action)

Overrides default java.util.Spliterator.forEachRemaining.

Doc from java.util.Spliterator.forEachRemaining.

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 ORDERED, actions are performed in encounter order. Exceptions thrown by the action are relayed to the caller.

Subsequent behavior of a spliterator is unspecified if the action throws an exception.

Parameters
action:Consumer<? super E>

The action

tryAdvanceback to summary
public boolean tryAdvance(Consumer<? super E> action)

Implements java.util.Spliterator.tryAdvance.

Doc from java.util.Spliterator.tryAdvance.

If a remaining element exists: performs the given action on it, returning true; else returns false. If this Spliterator is ORDERED the action is performed on the next element in encounter order. Exceptions thrown by the action are relayed to the caller.

Subsequent behavior of a spliterator is unspecified if the action throws an exception.

Parameters
action:Consumer<? super E>

The action whose operation is performed at-most once

Returns:boolean

false if no remaining elements existed upon entry to this method, else true.

trySplitback to summary
public Spliterator<E> trySplit()

Implements java.util.Spliterator.trySplit.

Doc from java.util.Spliterator.trySplit.

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 ORDERED, the returned Spliterator must cover a strict prefix of the elements.

Unless this Spliterator covers an infinite number of elements, repeated calls to trySplit() must eventually return null. Upon non-null return:

  • the value reported for estimateSize() before splitting, must, after splitting, be greater than or equal to estimateSize() for this and the returned Spliterator; and
  • if this Spliterator is SUBSIZED, then estimateSize() for this spliterator before splitting must be equal to the sum of estimateSize() for this and the returned Spliterator after splitting.

This method may return null for any reason, including emptiness, inability to split after traversal has commenced, data structure constraints, and efficiency considerations.

Returns:Spliterator<E>

a Spliterator covering some portion of the elements, or null if this spliterator cannot be split

java.util.concurrent back to summary

pack-priv Class LinkedBlockingQueue.Node<E>

extends Object
Class Inheritance

Linked list node class.

Field Summary

Modifier and TypeField and Description
pack-priv E
pack-priv LinkedBlockingQueue.Node<E>
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)

Constructor Summary

AccessConstructor and Description
pack-priv
Node(E x)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

itemback to summary
pack-priv E item
nextback to summary
pack-priv LinkedBlockingQueue.Node<E> 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)

Constructor Detail

Nodeback to summary
pack-priv Node(E x)