Class ManyToOneConcurrentArrayQueue<E>
- Type Parameters:
E- type of the elements stored in theQueue.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Queue<E>,Pipe<E>,QueuedPipe<E>
Unsafe.
Note: This queue breaks the contract for peek and poll in that it can return null when the queue has no item available but size could be greater than zero if an offer is in progress. This is due to the offer being a multiple step process which can start and be interrupted before completion, the thread will later be resumed and the offer process completes. Other methods, such as peek and poll, could spin internally waiting on the offer to complete to provide sequentially consistency across methods but this can have a detrimental effect in a resource starved system. This internal spinning eats up a CPU core and prevents other threads making progress resulting in latency spikes. To avoid this a more relaxed approach is taken in that an in-progress offer is not waited on to complete.
If you wish to check for empty then call AbstractConcurrentArrayQueue.isEmpty() rather than AbstractConcurrentArrayQueue.size() checking for zero.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longHead index.protected longCached head index.protected longShared cached head index.protected longTail index.Fields inherited from class org.agrona.concurrent.AbstractConcurrentArrayQueue
buffer, BUFFER_ARRAY_BASE, capacity, HEAD_OFFSET, SHARED_HEAD_CACHE_OFFSET, SHIFT_FOR_SCALE, TAIL_OFFSET -
Constructor Summary
ConstructorsConstructorDescriptionManyToOneConcurrentArrayQueue(int requestedCapacity) Constructs a queue with the requested capacity. -
Method Summary
Modifier and TypeMethodDescriptionintDrain the number of elements present in a collection at the time the operation starts.intDrain the minimum of a limit and the number of elements present in a collection at the time the operation starts.intdrainTo(Collection<? super E> target, int limit) Drain available elements into the providedCollectionup to a provided maximum limit of elements.booleanpoll()Methods inherited from class org.agrona.concurrent.AbstractConcurrentArrayQueue
add, addAll, addedCount, capacity, clear, contains, containsAll, element, isEmpty, iterator, peek, remainingCapacity, remove, remove, removeAll, removedCount, retainAll, sequenceToBufferOffset, size, toArray, toArrayMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
head
protected volatile long headHead index. -
tail
protected volatile long tailTail index. -
headCache
protected long headCacheCached head index.
-
-
Constructor Details
-
ManyToOneConcurrentArrayQueue
public ManyToOneConcurrentArrayQueue(int requestedCapacity) Constructs a queue with the requested capacity.- Parameters:
requestedCapacity- of the queue.
-
-
Method Details
-
offer
-
poll
-
drain
Drain the number of elements present in a collection at the time the operation starts.If possible, implementations should use smart batching to best handle burst traffic.
- Parameters:
elementConsumer-Consumerfor processing elements.- Returns:
- the number of elements drained.
-
drain
Drain the minimum of a limit and the number of elements present in a collection at the time the operation starts.If possible, implementations should use smart batching to best handle burst traffic.
- Parameters:
elementConsumer-Consumerfor processing elements.limit- maximum number of elements to be drained in a drain operation.- Returns:
- the number of elements drained.
-
drainTo
Drain available elements into the providedCollectionup to a provided maximum limit of elements.If possible, implementations should use smart batching to best handle burst traffic.
- Parameters:
target- in to which elements are drained.limit- maximum number of elements to be drained in a drain operation.- Returns:
- the number of elements actually drained.
-