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 long
Offset of thehead
field.protected static final long
Offset of thenext
field.protected org.agrona.concurrent.ManyToOneConcurrentLinkedQueuePadding1.Node<E>
Tail of the queue.protected static final long
Offset of thetail
field. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) void
clear()
boolean
boolean
containsAll
(Collection<?> c) element()
boolean
isEmpty()
iterator()
boolean
peek()
poll()
remove()
boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) int
size()
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, wait
Methods 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 thehead
field. -
TAIL_OFFSET
protected static final long TAIL_OFFSETOffset of thetail
field. -
NEXT_OFFSET
protected static final long NEXT_OFFSETOffset of thenext
field.
-
-
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() == 0
thenisEmpty()
is a better alternative.This operation is O(n) on the length of the linked chain.
- Specified by:
size
in interfaceCollection<E>
- Returns:
- an approximation for the size of the list.
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
-
iterator
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArray
in interfaceCollection<E>
-
remove
- Specified by:
remove
in interfaceCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
-
toString
-