Class SpscArrayQueue<E>

  • Type Parameters:
    E -
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Queue<E>, MessagePassingQueue<E>

    @SuppressAnimalSniffer
    public final class SpscArrayQueue<E>
    extends SpscArrayQueueL3Pad<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.

    • Constructor Detail

      • SpscArrayQueue

        public SpscArrayQueue​(int capacity)
    • Method Detail

      • offer

        public boolean offer​(E e)
        Called from a producer thread subject to the restrictions appropriate to the implementation and according to the Queue.offer(Object) interface.

        This implementation is correct for single producer thread use only.

        Returns:
        true if element was inserted into the queue, false iff full
      • poll

        public E poll()
        Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue.poll() interface.

        This implementation is correct for single consumer thread use only.

        Returns:
        a message from the queue if one is available, null iff empty
      • peek

        public E peek()
        Called from the consumer thread subject to the restrictions appropriate to the implementation and according to the Queue.peek() interface.

        This implementation is correct for single consumer thread use only.

        Returns:
        a message from the queue if one is available, null iff empty
      • size

        public int size()
        Description copied from interface: MessagePassingQueue
        This method's accuracy is subject to concurrent modifications happening as the size is estimated and as such is a best effort rather than absolute value. For some implementations this method may be O(n) rather than O(1).
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface MessagePassingQueue<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
        Returns:
        number of messages in the queue, between 0 and queue capacity or Integer.MAX_VALUE if not bounded
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: MessagePassingQueue
        This method's accuracy is subject to concurrent modifications happening as the observation is carried out.
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface MessagePassingQueue<E>
        Overrides:
        isEmpty in class java.util.AbstractCollection<E>
        Returns:
        true if empty, false otherwise
      • soProducerIndex

        private void soProducerIndex​(long v)
      • soConsumerIndex

        private void soConsumerIndex​(long v)
      • lvProducerIndex

        private long lvProducerIndex()
      • lvConsumerIndex

        private long lvConsumerIndex()