Class ManyToManyConcurrentArrayQueue<E>
- Type Parameters:
E- type of the elements stored in theQueue.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Queue<E>,Pipe<E>,QueuedPipe<E>
This is a Java port of Dmitry Vyukov's MPMC queue.
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. The poll method has similar properties for the multi-consumer implementation.
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
ConstructorsConstructorDescriptionManyToManyConcurrentArrayQueue(int requestedCapacity) Create a new queue with a bounded 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.booleanpeek()poll()Methods inherited from class org.agrona.concurrent.AbstractConcurrentArrayQueue
add, addAll, addedCount, capacity, clear, contains, containsAll, element, isEmpty, iterator, 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
-
ManyToManyConcurrentArrayQueue
public ManyToManyConcurrentArrayQueue(int requestedCapacity) Create a new queue with a bounded capacity. The requested capacity will be rounded up to the next positive power-of-two in size. That is if you request a capacity of 1000 then you will get 1024. If you request 1024 then that is what you will get.- Parameters:
requestedCapacity- of the queue which must be >= 2.- Throws:
IllegalArgumentException- if the requestedCapacity < 2.
-
-
Method Details
-
offer
-
poll
-
peek
-
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.
-