Top Description Inners Fields Constructors Methods
reactor.util.concurrent

pack-priv final Class MpscLinkedQueue<E>

extends AbstractQueue<E>
implements BiPredicate<E, E>
Class Inheritance
All Implemented Interfaces
java.util.function.BiPredicate
Type Parameters
<E>
the contained value type
Imports
java.util.AbstractQueue, .Iterator, .Objects, java.util.concurrent.atomic.AtomicReferenceFieldUpdater, java.util.function.BiPredicate, reactor.util.annotation.Nullable

A multi-producer single consumer unbounded queue.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final AtomicReferenceFieldUpdater<MpscLinkedQueue<E>, MpscLinkedQueue.LinkedQueueNode<E>>
private volatile MpscLinkedQueue.LinkedQueueNode<E>
private static final AtomicReferenceFieldUpdater<MpscLinkedQueue<E>, MpscLinkedQueue.LinkedQueueNode<E>>
private volatile MpscLinkedQueue.LinkedQueueNode<E>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Removes all of the elements from this queue.
public boolean
isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this collection contains no elements.
public Iterator<E>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

Returns an iterator over the elements in this collection.
public final boolean
offer(final E
the element to add
e
)

Implements java.util.Queue.offer.

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
public E
peek()

Implements java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
public E
poll()

Implements java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.
public boolean
remove(Object
element to be removed from this collection, if present
o
)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Collection.remove.

Removes a single instance of the specified element from this collection, if it is present (optional operation).
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements in this collection.
public boolean

Returns:

indicate whether elements has been successfully offered
test
(E
first element to offer
e1
,
E
second element to offer
e2
)

Implements java.util.function.BiPredicate.test.

This is an additional java.util.Queue extension for java.util.Queue#offer which allows atomically offer two elements at once.
Inherited from java.util.AbstractQueue:
addaddAllelementremove

Field Detail

CONSUMER_NODE_UPDATERback to summary
private static final AtomicReferenceFieldUpdater<MpscLinkedQueue<E>, MpscLinkedQueue.LinkedQueueNode<E>> CONSUMER_NODE_UPDATER
consumerNodeback to summary
private volatile MpscLinkedQueue.LinkedQueueNode<E> consumerNode
PRODUCER_NODE_UPDATERback to summary
private static final AtomicReferenceFieldUpdater<MpscLinkedQueue<E>, MpscLinkedQueue.LinkedQueueNode<E>> PRODUCER_NODE_UPDATER
producerNodeback to summary
private volatile MpscLinkedQueue.LinkedQueueNode<E> producerNode

Constructor Detail

MpscLinkedQueueback to summary
public MpscLinkedQueue()

Method Detail

clearback to summary
public void clear()

Overrides java.util.AbstractQueue.clear.

Implements java.util.Collection.clear.

Doc from java.util.AbstractQueue.clear.

Removes all of the elements from this queue. The queue will be empty after this call returns.

This implementation repeatedly invokes poll until it returns null.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Doc from java.util.Collection.isEmpty.

Returns true if this collection contains no elements.

Returns:boolean

true if this collection contains no elements

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

Doc from java.util.Collection.iterator.

Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

Returns:Iterator<E>

an Iterator over the elements in this collection

Annotations
@Override
offerback to summary
public final boolean offer(final E e)

Implements java.util.Queue.offer.

Doc from java.util.Queue.offer.

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add, which can fail to insert an element only by throwing an exception.

IMPLEMENTATION NOTES:
Offer is allowed from multiple threads.
Offer allocates a new node and:

  1. Swaps it atomically with current producer node (only one producer 'wins')
  2. Sets the new node as the node following from the swapped producer node
This works because each producer is guaranteed to 'plant' a new node and link the old node. No 2 producers can get the same producer node as part of XCHG guarantee.
Parameters
e:E

the element to add

Returns:boolean

true if the element was added to this queue, else false

Annotations
@Override
@SuppressWarnings:unchecked
See Also
java.util.Queue#offer(java.lang.Object)
peekback to summary
public E peek()

Implements java.util.Queue.peek.

Doc from java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

Annotations
@Nullable
@Override
pollback to summary
public E poll()

Implements java.util.Queue.poll.

Doc from java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.

IMPLEMENTATION NOTES:
Poll is allowed from a SINGLE thread.
Poll reads the next node from the consumerNode and:

  1. If it is null, the queue is assumed empty (though it might not be).
  2. If it is not null set it as the consumer node and return it's now evacuated value.
This means the consumerNode.value is always null, which is also the starting point for the queue. Because null values are not allowed to be offered this is the only node with it's value set to null at any one time.
Returns:E

the head of this queue, or null if this queue is empty

Annotations
@Nullable
@Override
See Also
java.util.Queue#poll()
removeback to summary
public boolean remove(Object o)

Overrides java.util.AbstractCollection.remove.

Implements java.util.Collection.remove.

Doc from java.util.Collection.remove.

Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that Objects.equals(o, e), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

Parameters
o:Object

element to be removed from this collection, if present

Returns:boolean

true if an element was removed as a result of this call

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Doc from java.util.Collection.size.

Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this collection

Annotations
@Override
testback to summary
public boolean test(E e1, E e2)

Implements java.util.function.BiPredicate.test.

This is an additional java.util.Queue extension for java.util.Queue#offer which allows atomically offer two elements at once.

IMPLEMENTATION NOTES:
Offer over test is allowed from multiple threads.
Offer over test allocates a two new nodes and:

  1. Swaps them atomically with current producer node (only one producer 'wins')
  2. Sets the new nodes as the node following from the swapped producer node
This works because each producer is guaranteed to 'plant' a new node and link the old node. No 2 producers can get the same producer node as part of XCHG guarantee.
Parameters
e1:E

first element to offer

e2:E

second element to offer

Returns:boolean

indicate whether elements has been successfully offered

Annotations
@Override
@SuppressWarnings:unchecked
See Also
java.util.Queue#offer(java.lang.Object)
reactor.util.concurrent back to summary

pack-priv final Class MpscLinkedQueue.LinkedQueueNode<E>

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private volatile MpscLinkedQueue.LinkedQueueNode<E>
private static final AtomicReferenceFieldUpdater<MpscLinkedQueue.LinkedQueueNode<E>, MpscLinkedQueue.LinkedQueueNode<E>>
private E

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv

Method Summary

Modifier and TypeMethod and Description
public E

Returns:

value
getAndNullValue
()

Gets the current value and nulls out the reference to it from this node.

public E
public MpscLinkedQueue.LinkedQueueNode<E>
public void
public void
spValue(E newValue)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nextback to summary
private volatile MpscLinkedQueue.LinkedQueueNode<E> next
NEXT_UPDATERback to summary
private static final AtomicReferenceFieldUpdater<MpscLinkedQueue.LinkedQueueNode<E>, MpscLinkedQueue.LinkedQueueNode<E>> NEXT_UPDATER
valueback to summary
private E value

Constructor Detail

LinkedQueueNodeback to summary
pack-priv LinkedQueueNode()
LinkedQueueNodeback to summary
pack-priv LinkedQueueNode(E val)

Method Detail

getAndNullValueback to summary
public E getAndNullValue()

Gets the current value and nulls out the reference to it from this node.

Returns:E

value

Annotations
@Nullable
lpValueback to summary
public E lpValue()
Annotations
@Nullable
lvNextback to summary
public MpscLinkedQueue.LinkedQueueNode<E> lvNext()
Annotations
@Nullable
soNextback to summary
public void soNext(MpscLinkedQueue.LinkedQueueNode<E> n)
spValueback to summary
public void spValue(E newValue)