Class SpscArrayQueue<E>

java.lang.Object
java.util.concurrent.atomic.AtomicReferenceArray<E>
io.reactivex.rxjava4.operators.SpscArrayQueue<E>
Type Parameters:
E - the element type of the queue
All Implemented Interfaces:
SimplePlainQueue<E>, SimpleQueue<E>, Serializable

public final class SpscArrayQueue<E> extends AtomicReferenceArray<E> implements SimplePlainQueue<E>
A Single-Producer-Single-Consumer queue backed by a pre-allocated buffer.

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.

Since:
3.1.1
See Also:
  • Constructor Details

    • SpscArrayQueue

      public SpscArrayQueue(int capacity)
      Constructs an array-backed queue with the given capacity rounded up to the next power of 2 size.
      Parameters:
      capacity - the maximum number of elements the queue would hold, rounded up to the next power of 2
  • Method Details

    • offer

      public boolean offer(E e)
      Description copied from interface: SimpleQueue
      Atomically enqueue a single value.
      Specified by:
      offer in interface SimpleQueue<E>
      Parameters:
      e - the value to enqueue, not null
      Returns:
      true if successful, false if the value was not enqueued likely due to reaching the queue capacity)
    • offer

      public boolean offer(E v1, E v2)
      Description copied from interface: SimpleQueue
      Atomically enqueue two values.
      Specified by:
      offer in interface SimpleQueue<E>
      Parameters:
      v1 - the first value to enqueue, not null
      v2 - the second value to enqueue, not null
      Returns:
      true if successful, false if the value was not enqueued likely due to reaching the queue capacity)
    • poll

      @Nullable public E poll()
      Description copied from interface: SimpleQueue
      Tries to dequeue a value (non-null) or returns null if the queue is empty.

      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.

      Specified by:
      poll in interface SimplePlainQueue<E>
      Specified by:
      poll in interface SimpleQueue<E>
      Returns:
      the item or null to indicate an empty queue
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: SimpleQueue
      Returns true if the queue is empty.

      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).

      Specified by:
      isEmpty in interface SimpleQueue<E>
      Returns:
      true if the queue is empty
    • clear

      public void clear()
      Description copied from interface: SimpleQueue
      Removes all enqueued items from this queue.
      Specified by:
      clear in interface SimpleQueue<E>