This is a classic "bounded buffer", in which a
fixed-sized array holds elements inserted by producers and
extracted by consumers. Once created, the capacity cannot be
changed. Attempts to put
an element into a full queue
will result in the operation blocking; attempts to take
an
element from an empty queue will similarly block.
This class supports an optional fairness policy for ordering
waiting producer and consumer threads. By default, this ordering
is not guaranteed. However, a queue constructed with fairness set
to true
grants threads access in FIFO order. Fairness
generally decreases throughput but reduces variability and avoids
starvation.
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 | ArrayBlockingQueue.
Iterator for ArrayBlockingQueue. |
pack-priv class | ArrayBlockingQueue.
Shared data between iterators and their queue, allowing queue modifications to update iterators when elements are removed. |
Modifier and Type | Field and Description |
---|---|
pack-priv int | count
Number of elements in the queue |
pack-priv final Object[] | items
The queued items |
pack-priv transient ArrayBlockingQueue | itrs
Shared state for currently active iterators, or null if there are known not to be any. |
pack-priv final ReentrantLock | lock
Main lock guarding all access |
private final Condition | notEmpty
Condition for waiting takes |
private final Condition | notFull
Condition for waiting puts |
pack-priv int | putIndex
items index for next put, offer, or add |
private static final long | serialVersionUID
Serialization ID. This class relies on default serialization even for the items array, which is default-serialized, even if it is empty. |
pack-priv int | takeIndex
items index for next take, poll, peek or remove |
Access | Constructor and Description |
---|---|
public | ArrayBlockingQueue(int
the capacity of this queue capacity)Creates an |
public | ArrayBlockingQueue(int
the capacity of this queue capacity, boolean if fair)true then queue accesses for threads blocked
on insertion or removal, are processed in FIFO order;
if false the access order is unspecified.Creates an |
public | ArrayBlockingQueue(int
the capacity of this queue capacity, boolean if fair, Collection<? extends E> true then queue accesses for threads blocked
on insertion or removal, are processed in FIFO order;
if false the access order is unspecified.the collection of elements to initially contain c)Creates an |
Modifier and Type | Method and Description |
---|---|
public boolean | add(E
the element to add e)Overrides java. Implements java. true upon success and throwing an
IllegalStateException if this queue is full.
|
private boolean | |
private boolean | bulkRemoveModified(Predicate<? super E> filter, final int
valid index of first element to be deleted beg)Helper for bulkRemove, in case of at least one deletion. |
pack-priv void | |
private static void | circularClear(Object[] items, int i, int end)
Nulls out slots starting at array index i, up to index end. |
public void | clear()
Overrides java. Implements java. |
public boolean | Returns: true if this queue contains the specified elementobject to be checked for containment in this queue o)Overrides java. Implements java. true if this queue contains the specified element.
|
pack-priv static final int | |
private E | |
private int | distanceNonEmpty(int i, int j)
Returns circular distance from i to j, disambiguating i == j to items.length; never returns 0. |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c)Implements java. |
public int | drainTo(Collection<? super E>
the collection to transfer elements into c, int the maximum number of elements to transfer maxElements)Implements java. |
private void | |
public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Iterable
until all elements have been processed or the action throws an
exception.
|
pack-priv static final int | |
private boolean | |
private static boolean | |
pack-priv final E | |
pack-priv static <E> E | |
public Iterator | Returns: an iterator over the elements in this queue in proper sequenceImplements abstract java. Implements java. |
private static long[] | |
public boolean | offer(E
the element to add e)Implements java. true upon success and false if this queue
is full.
|
public boolean | offer(E
the 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. |
public E | peek()
Implements java. null if this queue is empty.
|
public E | poll()
Implements java. null if this queue is empty.
|
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. |
public void | put(E
the element to add e)Implements java. |
private void | readObject(ObjectInputStream
the stream s)Reconstitutes this queue from a stream (that is, deserializes it). |
public int | remainingCapacity()
Implements java. |
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. |
public boolean | removeAll(Collection<?>
collection containing elements to be removed from this collection c)Overrides java. Implements java. |
pack-priv void | |
public boolean | removeIf(Predicate<? super E>
a predicate which returns filter)true for elements to be
removedOverrides default java. |
public boolean | retainAll(Collection<?>
collection containing elements to be retained in this collection c)Overrides java. Implements java. |
private static void | |
public int | Returns: the number of elements in this queueImplements abstract java. Implements java. |
public Spliterator | Returns: aSpliterator over the elements in this queueOverrides default java. Spliterator over the elements in this queue.
|
public E | take()
Implements java. |
public Object[] | Returns: an array containing all of the elements in this queueOverrides java. Implements java. |
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. |
public String | toString()
Overrides java. |