Class ManyToOneConcurrentLinkedQueue<E>
- Type Parameters:
E- element type in the queue.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Queue<E>
Queue that can be used from many producers and a single consumer.
This is a Java port of Dmitry Vyukov's MPSC linked 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.
If you wish to check for empty then call isEmpty() rather than size() checking for zero.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.agrona.concurrent.ManyToOneConcurrentLinkedQueuePadding1.Node<E>Head of queue.protected static final longOffset of theheadfield.protected static final longOffset of thenextfield.protected org.agrona.concurrent.ManyToOneConcurrentLinkedQueuePadding1.Node<E>Tail of the queue.protected static final longOffset of thetailfield. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) voidclear()booleanbooleancontainsAll(Collection<?> c) element()booleanisEmpty()iterator()booleanpeek()poll()remove()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Size can be considered an approximation on a moving list.Object[]toArray()<T> T[]toArray(T[] a) toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
head
Head of queue. -
tail
Tail of the queue. -
HEAD_OFFSET
protected static final long HEAD_OFFSETOffset of theheadfield. -
TAIL_OFFSET
protected static final long TAIL_OFFSETOffset of thetailfield. -
NEXT_OFFSET
protected static final long NEXT_OFFSETOffset of thenextfield.
-
-
Constructor Details
-
ManyToOneConcurrentLinkedQueue
public ManyToOneConcurrentLinkedQueue()Constructs an empty queue.
-
-
Method Details
-
add
-
offer
-
remove
-
poll
-
element
-
peek
-
size
public int size()Size can be considered an approximation on a moving list. It is only really stable when the consumer is inactive. If you want to check forqueue.size() == 0thenisEmpty()is a better alternative.This operation is O(n) on the length of the linked chain.
- Specified by:
sizein interfaceCollection<E>- Returns:
- an approximation for the size of the list.
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>
-
iterator
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-
toString
-