Class SpscLinkedArrayQueue<T>

  • Type Parameters:
    T - the element type, not null
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.Queue<T>

    public final class SpscLinkedArrayQueue<T>
    extends java.lang.Object
    implements java.util.Queue<T>
    A single-producer single-consumer array-backed queue which can allocate new arrays in case the consumer is slower than the producer.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T e)  
      boolean addAll​(java.util.Collection<? extends T> c)  
      private void adjustLookAheadStep​(int capacity)  
      private static int calcDirectOffset​(int index)  
      private static int calcWrappedOffset​(long index, int mask)  
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      T element()  
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      private long lpConsumerIndex()  
      private long lpProducerIndex()  
      private long lvConsumerIndex()  
      private static <E> java.lang.Object lvElement​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> buffer, int offset)  
      private java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> lvNext​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> curr)  
      private long lvProducerIndex()  
      private T newBufferPeek​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> nextBuffer, long index, int mask)  
      private T newBufferPoll​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> nextBuffer, long index, int mask)  
      boolean offer​(T e)
      boolean offer​(T first, T second)
      Offer two elements at the same time.
      T peek()
      T poll()
      T remove()  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      private void resize​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> oldBuffer, long currIndex, int offset, T e, long mask)  
      boolean retainAll​(java.util.Collection<?> c)  
      int size()  
      private void soConsumerIndex​(long v)  
      private static void soElement​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> buffer, int offset, java.lang.Object e)  
      private void soNext​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> curr, java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> next)  
      private void soProducerIndex​(long v)  
      java.lang.Object[] toArray()  
      <E> E[] toArray​(E[] a)  
      private boolean writeToQueue​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> buffer, T e, long index, int offset)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • MAX_LOOK_AHEAD_STEP

        static final int MAX_LOOK_AHEAD_STEP
      • producerIndex

        final java.util.concurrent.atomic.AtomicLong producerIndex
      • producerLookAheadStep

        int producerLookAheadStep
      • producerLookAhead

        long producerLookAhead
      • producerMask

        int producerMask
      • producerBuffer

        java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> producerBuffer
      • consumerMask

        int consumerMask
      • consumerBuffer

        java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> consumerBuffer
      • consumerIndex

        final java.util.concurrent.atomic.AtomicLong consumerIndex
      • HAS_NEXT

        private static final java.lang.Object HAS_NEXT
    • Constructor Detail

      • SpscLinkedArrayQueue

        public SpscLinkedArrayQueue​(int bufferSize)
    • Method Detail

      • offer

        public boolean offer​(T e)

        This implementation is correct for single producer thread use only.

        Specified by:
        offer in interface java.util.Queue<T>
      • writeToQueue

        private boolean writeToQueue​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> buffer,
                                     T e,
                                     long index,
                                     int offset)
      • resize

        private void resize​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> oldBuffer,
                            long currIndex,
                            int offset,
                            T e,
                            long mask)
      • soNext

        private void soNext​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> curr,
                            java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> next)
      • lvNext

        private java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> lvNext​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> curr)
      • poll

        public T poll()

        This implementation is correct for single consumer thread use only.

        Specified by:
        poll in interface java.util.Queue<T>
      • newBufferPoll

        private T newBufferPoll​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> nextBuffer,
                                long index,
                                int mask)
      • peek

        public T peek()

        This implementation is correct for single consumer thread use only.

        Specified by:
        peek in interface java.util.Queue<T>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
      • newBufferPeek

        private T newBufferPeek​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> nextBuffer,
                                long index,
                                int mask)
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
      • adjustLookAheadStep

        private void adjustLookAheadStep​(int capacity)
      • lvProducerIndex

        private long lvProducerIndex()
      • lvConsumerIndex

        private long lvConsumerIndex()
      • lpProducerIndex

        private long lpProducerIndex()
      • lpConsumerIndex

        private long lpConsumerIndex()
      • soProducerIndex

        private void soProducerIndex​(long v)
      • soConsumerIndex

        private void soConsumerIndex​(long v)
      • calcWrappedOffset

        private static int calcWrappedOffset​(long index,
                                             int mask)
      • calcDirectOffset

        private static int calcDirectOffset​(int index)
      • soElement

        private static void soElement​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> buffer,
                                      int offset,
                                      java.lang.Object e)
      • lvElement

        private static <E> java.lang.Object lvElement​(java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Object> buffer,
                                                      int offset)
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
      • toArray

        public <E> E[] toArray​(E[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> c)
        Specified by:
        addAll in interface java.util.Collection<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
      • add

        public boolean add​(T e)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.Queue<T>
      • remove

        public T remove()
        Specified by:
        remove in interface java.util.Queue<T>
      • element

        public T element()
        Specified by:
        element in interface java.util.Queue<T>
      • offer

        public boolean offer​(T first,
                             T second)
        Offer two elements at the same time.

        Don't use the regular offer() with this at all!

        Parameters:
        first -
        second -
        Returns:
        always true