Package org.eclipse.jetty.util
Class BlockingArrayQueue<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.eclipse.jetty.util.BlockingArrayQueue<E>
- Type Parameters:
E- The element type
- All Implemented Interfaces:
Iterable<E>,Collection<E>,BlockingQueue<E>,List<E>,Queue<E>,SequencedCollection<E>
A BlockingQueue backed by a circular array capable or growing.
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 ArrayBlockingQueue, 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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object[]private final intprivate final Lockprivate final int[]Array that holds the head and tail indexes, separated by a cache line to avoid false sharingprivate final intprivate final Conditionprivate final AtomicIntegerprivate final Lockstatic final intDefault initial capacity, 128.static final intDefault growth factor, 64.private static final intThe head offset in the_indexesarray, displaced by 15 slots to avoid false sharing with the array length (stored before the first element of the array itself).private static final intThe tail offset in the_indexesarray, displaced by 16 slots from the head to avoid false sharing with it.Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionCreates an unboundedBlockingArrayQueuewith default initial capacity and grow factor.BlockingArrayQueue(int maxCapacity) Creates a boundedBlockingArrayQueuethat does not grow.BlockingArrayQueue(int capacity, int growBy) Creates an unboundedBlockingArrayQueuethat grows by the given parameter.BlockingArrayQueue(int capacity, int growBy, int maxCapacity) Create a boundedBlockingArrayQueuethat grows by the given parameter. -
Method Summary
Modifier and TypeMethodDescriptionvoidbooleanvoidclear()intdrainTo(Collection<? super E> c) intdrainTo(Collection<? super E> c, int maxElements) element()get(int index) intintprivate booleangrow()iterator()listIterator(int index) booleanbooleanpeek()poll()voidintremove()remove(int index) booleanintsize()take()Methods inherited from class java.util.AbstractList
addAll, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.BlockingQueue
containsMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, isEmpty, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator, toArray, toArray
-
Field Details
-
HEAD_OFFSET
private static final int HEAD_OFFSETThe head offset in the_indexesarray, displaced by 15 slots to avoid false sharing with the array length (stored before the first element of the array itself). -
TAIL_OFFSET
private static final int TAIL_OFFSETThe tail offset in the_indexesarray, displaced by 16 slots from the head to avoid false sharing with it. -
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITYDefault initial capacity, 128.- See Also:
-
DEFAULT_GROWTH
public static final int DEFAULT_GROWTHDefault growth factor, 64.- See Also:
-
_maxCapacity
private final int _maxCapacity -
_growCapacity
private final int _growCapacity -
_indexes
private final int[] _indexesArray that holds the head and tail indexes, separated by a cache line to avoid false sharing -
_tailLock
-
_size
-
_headLock
-
_notEmpty
-
_elements
-
-
Constructor Details
-
BlockingArrayQueue
public BlockingArrayQueue()Creates an unboundedBlockingArrayQueuewith default initial capacity and grow factor.- See Also:
-
BlockingArrayQueue
public BlockingArrayQueue(int maxCapacity) Creates a boundedBlockingArrayQueuethat does not grow. The capacity of the queue is fixed and equal to the given parameter.- Parameters:
maxCapacity- the maximum capacity
-
BlockingArrayQueue
public BlockingArrayQueue(int capacity, int growBy) Creates an unboundedBlockingArrayQueuethat grows by the given parameter.- Parameters:
capacity- the initial capacitygrowBy- the growth factor
-
BlockingArrayQueue
public BlockingArrayQueue(int capacity, int growBy, int maxCapacity) Create a boundedBlockingArrayQueuethat grows by the given parameter.- Parameters:
capacity- the initial capacitygrowBy- the growth factormaxCapacity- the maximum capacity
-
-
Method Details
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>
-
iterator
-
poll
-
peek
-
remove
-
element
-
offer
-
add
- Specified by:
addin interfaceBlockingQueue<E>- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractList<E>
-
put
- Specified by:
putin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
offer
- Specified by:
offerin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
take
- Specified by:
takein interfaceBlockingQueue<E>- Throws:
InterruptedException
-
poll
- Specified by:
pollin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
remove
- Specified by:
removein interfaceBlockingQueue<E>- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>
-
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacityin interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>
-
get
-
add
-
set
-
remove
-
listIterator
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>
-
getCapacity
public int getCapacity()- Returns:
- the current capacity of this queue
-
getMaxCapacity
public int getMaxCapacity()- Returns:
- the max capacity of this queue, or -1 if this queue is unbounded
-
grow
private boolean grow()
-