This queue is uses a variant of the two lock queue algorithm to provide an efficient queue or list backed by a growable circular array.
Unlike java., this class is able to grow and provides a blocking put call.
The queue has both a capacity (the size of the array currently allocated) and a max capacity (the maximum size that may be allocated), which defaults to
Integer#MAX_VALUE.
| Modifier and Type | Class and Description |
|---|---|
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private Object[] | |
| private final int | |
| private final Lock | |
| private final int[] | _indexes
Array that holds the head and tail indexes, separated by a cache line to avoid false sharing |
| private final int | |
| private final Condition | |
| private final AtomicInteger | |
| private final Lock | |
| public static final int | DEFAULT_CAPACITY
Default initial capacity, 128. |
| public static final int | DEFAULT_GROWTH
Default growth factor, 64. |
| private static final int | HEAD_OFFSET
The head offset in the |
| private static final int | TAIL_OFFSET
The tail offset in the |
| Access | Constructor and Description |
|---|---|
| public | BlockingArrayQueue()
Creates an unbounded |
| public | BlockingArrayQueue(int
the maximum capacity maxCapacity)Creates a bounded |
| public | BlockingArrayQueue(int
the initial capacity capacity, int the growth factor growBy)Creates an unbounded |
| public | BlockingArrayQueue(int
the initial capacity capacity, int the growth factor growBy, int the maximum capacity maxCapacity)Create a bounded |
| 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 no space is currently available.
|
| public void | add(int
index at which the specified element is to be inserted index, E element to be inserted e)Overrides java. Implements java. |
| public void | clear()
Overrides java. Implements java. |
| 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. |
| public E | element()
Implements java. |
| public E | get(int
index of the element to return index)Implements abstract java. Implements java. |
| public int | |
| public int | Returns: the max capacity of this queue, or -1 if this queue is unboundedGet the max capacity of this queue, or -1 if this queue is unbounded. |
| private boolean | |
| public Iterator | iterator()
Overrides java. Implements java. |
| public ListIterator | listIterator(int
index of the first element to be returned from the
list iterator (by a call to index)next)Overrides java. Implements java. |
| public boolean | offer(E
the element to add e)Implements java. true upon success and false if no space is currently
available.
|
| public boolean | offer(E
the element to add o, long how long to wait before giving up, in units of
timeout, TimeUnit unita 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
time, TimeUnit unita unit)TimeUnit determining how to interpret the
timeout parameterImplements java. |
| public void | put(E
the element to add o)Implements java. |
| public int | remainingCapacity()
Implements java. Integer.MAX_VALUE if there is no intrinsic
limit.
|
| public E | |
| public E | remove(int
the index of the element to be removed index)Overrides java. Implements java. |
| public boolean | remove(Object
element to be removed from this queue, if present o)Overrides java. Implements java. |
| public E | set(int
index of the element to replace index, E element to be stored at the specified position e)Overrides java. Implements java. |
| public int | size()
Implements abstract java. Implements java. |
| public E | take()
Implements java. |
| _elements | back to summary |
|---|---|
| private Object[] _elements | |
| _growCapacity | back to summary |
|---|---|
| private final int _growCapacity | |
| _headLock | back to summary |
|---|---|
| private final Lock _headLock | |
| _indexes | back to summary |
|---|---|
| private final int[] _indexes Array that holds the head and tail indexes, separated by a cache line to avoid false sharing | |
| _maxCapacity | back to summary |
|---|---|
| private final int _maxCapacity | |
| _notEmpty | back to summary |
|---|---|
| private final Condition _notEmpty | |
| _size | back to summary |
|---|---|
| private final AtomicInteger _size | |
| _tailLock | back to summary |
|---|---|
| private final Lock _tailLock | |
| DEFAULT_CAPACITY | back to summary |
|---|---|
| public static final int DEFAULT_CAPACITY Default initial capacity, 128. | |
| DEFAULT_GROWTH | back to summary |
|---|---|
| public static final int DEFAULT_GROWTH Default growth factor, 64. | |
| HEAD_OFFSET | back to summary |
|---|---|
| private static final int HEAD_OFFSET The head offset in the | |
| TAIL_OFFSET | back to summary |
|---|---|
| private static final int TAIL_OFFSET The tail offset in the | |
| BlockingArrayQueue | back to summary |
|---|---|
| public BlockingArrayQueue() Creates an unbounded
| |
| BlockingArrayQueue | back to summary |
|---|---|
| public BlockingArrayQueue(int maxCapacity) Creates a bounded
| |
| BlockingArrayQueue | back to summary |
|---|---|
| public BlockingArrayQueue(int capacity, int growBy) Creates an unbounded
| |
| BlockingArrayQueue | back to summary |
|---|---|
| public BlockingArrayQueue(int capacity, int growBy, int maxCapacity) Create a bounded
| |
| add | back to summary |
|---|---|
| public boolean add(E e) Overrides java. Implements java. Doc from java. Inserts the specified element into this queue if it is possible to do
so immediately without violating capacity restrictions, returning
| |
| add | back to summary |
|---|---|
| public void add(int index, E e) Overrides java. Implements java. Doc from java. Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Removes all of the elements from this collection (optional operation). The collection will be empty after this method returns.
| |
| 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
| |
| element | back to summary |
|---|---|
| public E element() Implements java. Doc from java. Retrieves, but does not remove, the head of this queue. This method
differs from
| |
| get | back to summary |
|---|---|
| public E get(int index) Implements abstract java. Implements java. Doc from java. Returns the element at the specified position in this list.
| |
| getCapacity | back to summary |
|---|---|
| public int getCapacity() Get the current capacity of this queue.
| |
| getMaxCapacity | back to summary |
|---|---|
| public int getMaxCapacity() Get the max capacity of this queue, or -1 if this queue is unbounded.
| |
| grow | back to summary |
|---|---|
| private boolean grow() | |
| iterator | back to summary |
|---|---|
| public Iterator Overrides java. Implements java. Doc from java. Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee). | |
| listIterator | back to summary |
|---|---|
| public ListIterator Overrides java. Implements java. Doc from java. Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
The specified index indicates the first element that would be
returned by an initial call to
| |
| offer | back to summary |
|---|---|
| public boolean offer(E e) Implements java. Doc from java. Inserts the specified element into this queue if it is possible to do
so immediately without violating capacity restrictions, returning
| |
| offer | back to summary |
|---|---|
| public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException Implements java. Doc from java. Inserts the specified element into this queue, waiting up to the specified wait time if necessary for space to become available.
| |
| 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() Implements java. Doc from java. Retrieves and removes the head of this queue,
or returns
| |
| poll | back to summary |
|---|---|
| public E poll(long time, 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.
| |
| put | back to summary |
|---|---|
| public void put(E o) throws InterruptedException Implements java. Doc from java. Inserts the specified element into this queue, waiting if necessary for space to become available.
| |
| remainingCapacity | back to summary |
|---|---|
| public int remainingCapacity() Implements java. Doc from java. Returns the number of additional elements that this queue can ideally
(in the absence of memory or resource constraints) accept without
blocking, or Note that you cannot always tell if an attempt to insert
an element will succeed by inspecting
| |
| remove | back to summary |
|---|---|
| public E remove() Implements java. Doc from java. Retrieves and removes the head of this queue. This method differs
from
| |
| remove | back to summary |
|---|---|
| public E remove(int index) Overrides java. Implements java. Doc from java. Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
| |
| remove | back to summary |
|---|---|
| public boolean remove(Object o) Overrides java. Implements java. Doc from java. Removes a single instance of the specified element from this queue,
if it is present. More formally, removes an element | |
| set | back to summary |
|---|---|
| public E set(int index, E e) Overrides java. Implements java. Doc from java. Replaces the element at the specified position in this list with the specified element (optional operation).
| |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this collection. If this collection
contains more than
| |
| 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.
| |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private final Object[] |
| Modifier and Type | Method and Description |
|---|---|
| public void | add(E
the element to insert e)Implements java. |
| public boolean | hasNext()
Implements java. true if this list iterator has more elements when
traversing the list in the forward direction.
|
| public boolean | hasPrevious()
Implements java. true if this list iterator has more elements when
traversing the list in the reverse direction.
|
| public E | next()
Implements java. |
| public int | nextIndex()
Implements java. next.
|
| public E | previous()
Implements java. |
| public int | previousIndex()
Implements java. previous.
|
| public void | remove()
Implements java. next or previous (optional operation).
|
| public void | set(E
the element with which to replace the last element returned by
e)next or previousImplements java. next or
previous with the specified element (optional operation).
|
| _cursor | back to summary |
|---|---|
| private int _cursor | |
| _elements | back to summary |
|---|---|
| private final Object[] _elements | |
| Itr | back to summary |
|---|---|
| public Itr(Object[] elements, int offset) | |
| add | back to summary |
|---|---|
| public void add(E e) Implements java. Doc from java. Inserts the specified element into the list (optional operation).
The element is inserted immediately before the element that
would be returned by
| |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| hasPrevious | back to summary |
|---|---|
| public boolean hasPrevious() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public E next() Implements java. Doc from java. Returns the next element in the list and advances the cursor position.
This method may be called repeatedly to iterate through the list,
or intermixed with calls to
| |
| nextIndex | back to summary |
|---|---|
| public int nextIndex() Implements java. Doc from java. Returns the index of the element that would be returned by a
subsequent call to
| |
| previous | back to summary |
|---|---|
| public E previous() Implements java. Doc from java. Returns the previous element in the list and moves the cursor
position backwards. This method may be called repeatedly to
iterate through the list backwards, or intermixed with calls to
| |
| previousIndex | back to summary |
|---|---|
| public int previousIndex() Implements java. Doc from java. Returns the index of the element that would be returned by a
subsequent call to
| |
| remove | back to summary |
|---|---|
| public void remove() Implements java. Doc from java. Removes from the list the last element that was returned by
| |
| set | back to summary |
|---|---|
| public void set(E e) Implements java. Doc from java. Replaces the last element returned by
| |