E
- the element type of the queuepublic final class SpscArrayQueue<E> extends AtomicReferenceArray<E> implements SimplePlainQueue<E>
This implementation is a mashup of the Fast Flow
algorithm with an optimization of the offer method taken from the BQueue algorithm (a variation on Fast
Flow), and adjusted to comply with Queue.offer semantics with regards to capacity.
For convenience the relevant papers are available in the resources folder:
2010 - Pisa - SPSC Queues on Shared Cache Multi-Core Systems.pdf
2012 - Junchang- BQueue- Efficient and Practical Queuing.pdf
This implementation is wait free.
Constructor and Description |
---|
SpscArrayQueue(int capacity)
Constructs an array-backed queue with the given capacity rounded
up to the next power of 2 size.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all enqueued items from this queue.
|
boolean |
isEmpty()
Returns true if the queue is empty.
|
boolean |
offer(E e)
Atomically enqueue a single value.
|
boolean |
offer(E v1,
E v2)
Atomically enqueue two values.
|
E |
poll()
Tries to dequeue a value (non-null) or returns null if
the queue is empty.
|
accumulateAndGet, compareAndSet, get, getAndAccumulate, getAndSet, getAndUpdate, lazySet, length, set, toString, updateAndGet, weakCompareAndSet
public SpscArrayQueue(int capacity)
capacity
- the maximum number of elements the queue would hold,
rounded up to the next power of 2public boolean offer(E e)
SimpleQueue
offer
in interface SimpleQueue<E>
e
- the value to enqueue, not nullpublic boolean offer(E v1, E v2)
SimpleQueue
offer
in interface SimpleQueue<E>
v1
- the first value to enqueue, not nullv2
- the second value to enqueue, not null@Nullable public E poll()
SimpleQueue
If the producer uses SimpleQueue.offer(Object, Object)
and
when polling in pairs, if the first poll() returns a non-null
item, the second poll() is guaranteed to return a non-null item
as well.
poll
in interface SimplePlainQueue<E>
poll
in interface SimpleQueue<E>
public boolean isEmpty()
SimpleQueue
Note however that due to potential fused functions in SimpleQueue.poll()
it is possible this method returns false but then poll() returns null
because the fused function swallowed the available item(s).
isEmpty
in interface SimpleQueue<E>
public void clear()
SimpleQueue
clear
in interface SimpleQueue<E>