E
- type of the elements stored in the Queue
.public class ManyToManyConcurrentArrayQueue<E> extends AbstractConcurrentArrayQueue<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 multi-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.
Modifier and Type | Field and Description |
---|---|
protected long |
head |
protected long |
headCache |
protected long |
sharedHeadCache |
protected long |
tail |
buffer, BUFFER_ARRAY_BASE, capacity, HEAD_OFFSET, SHARED_HEAD_CACHE_OFFSET, SHIFT_FOR_SCALE, TAIL_OFFSET
Constructor and Description |
---|
ManyToManyConcurrentArrayQueue(int requestedCapacity)
Create a new queue with a bounded capacity.
|
Modifier and Type | Method and Description |
---|---|
int |
drain(Consumer<E> elementConsumer)
Drain the number of elements present in a collection at the time the operation starts.
|
int |
drain(Consumer<E> elementConsumer,
int limit)
Drain the minimum of a limit and the number of elements present in a collection at the time the operation starts.
|
int |
drainTo(Collection<? super E> target,
int limit)
Drain available elements into the provided
Collection up to a provided maximum limit of
elements. |
boolean |
offer(E e) |
E |
peek() |
E |
poll() |
add, addAll, addedCount, capacity, clear, contains, containsAll, element, isEmpty, iterator, remainingCapacity, remove, remove, removeAll, removedCount, retainAll, sequenceToBufferOffset, size, toArray, toArray
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals, hashCode, parallelStream, removeIf, spliterator, stream
protected volatile long head
protected volatile long tail
protected long headCache
protected volatile long sharedHeadCache
public ManyToManyConcurrentArrayQueue(int requestedCapacity)
requestedCapacity
- of the queue which must be >= 2.IllegalArgumentException
- if the requestedCapacity < 2.public boolean offer(E e)
public E poll()
public E peek()
public int drain(Consumer<E> elementConsumer)
Pipe
If possible, implementations should use smart batching to best handle burst traffic.
elementConsumer
- Consumer
for processing elements.public int drain(Consumer<E> elementConsumer, int limit)
Pipe
If possible, implementations should use smart batching to best handle burst traffic.
elementConsumer
- Consumer
for processing elements.limit
- maximum number of elements to be drained in a drain operation.public int drainTo(Collection<? super E> target, int limit)
Pipe
Collection
up to a provided maximum limit of
elements.
If possible, implementations should use smart batching to best handle burst traffic.
target
- in to which elements are drained.limit
- maximum number of elements to be drained in a drain operation.Copyright © 2014-2020 Real Logic Limited. All Rights Reserved.