Class ManyToManyConcurrentArrayQueue<E>

Type Parameters:
E - type of the elements stored in the Queue.
All Implemented Interfaces:
Iterable<E>, Collection<E>, Queue<E>, Pipe<E>, QueuedPipe<E>

public class ManyToManyConcurrentArrayQueue<E> extends AbstractConcurrentArrayQueue<E>
Many producer to many consumer concurrent queue that is array backed.

This is a Java port of Dmitry Vyukov's MPMC queue.

Note: This queue breaks the contract for peek and poll in that it can return null when the queue has no item available but size could be greater than zero if an offer is in progress. This is due to the offer being a multiple step process which can start and be interrupted before completion, the thread will later be resumed and the offer process completes. Other methods, such as peek and poll, could spin internally waiting on the offer to complete to provide sequentially consistency across methods but this can have a detrimental effect in a resource starved system. This internal spinning eats up a CPU core and prevents other threads making progress resulting in latency spikes. To avoid this a more relaxed approach is taken in that an in-progress offer is not waited on to complete. The poll method has similar properties for the multi-consumer implementation.

If you wish to check for empty then call AbstractConcurrentArrayQueue.isEmpty() rather than AbstractConcurrentArrayQueue.size() checking for zero.

  • Field Details

    • SEQUENCES_ARRAY_BASE

      private static final int SEQUENCES_ARRAY_BASE
    • sequences

      private final long[] sequences
  • Constructor Details

    • ManyToManyConcurrentArrayQueue

      public ManyToManyConcurrentArrayQueue(int requestedCapacity)
      Create a new queue with a bounded capacity. The requested capacity will be rounded up to the next positive power-of-two in size. That is if you request a capacity of 1000 then you will get 1024. If you request 1024 then that is what you will get.
      Parameters:
      requestedCapacity - of the queue which must be >= 2.
      Throws:
      IllegalArgumentException - if the requestedCapacity < 2.
  • Method Details

    • offer

      public boolean offer(E e)
    • poll

      public E poll()
    • peek

      public E peek()
      Specified by:
      peek in interface Queue<E>
      Overrides:
      peek in class AbstractConcurrentArrayQueue<E>
    • drain

      public int drain(Consumer<E> elementConsumer)
      Drain the number of elements present in a collection at the time the operation starts.

      If possible, implementations should use smart batching to best handle burst traffic.

      Parameters:
      elementConsumer - Consumer for processing elements.
      Returns:
      the number of elements drained.
    • drain

      public int drain(Consumer<E> elementConsumer, int limit)
      Drain the minimum of a limit and the number of elements present in a collection at the time the operation starts.

      If possible, implementations should use smart batching to best handle burst traffic.

      Parameters:
      elementConsumer - Consumer for processing elements.
      limit - maximum number of elements to be drained in a drain operation.
      Returns:
      the number of elements drained.
    • drainTo

      public int drainTo(Collection<? super E> target, int limit)
      Drain available elements into the provided Collection up to a provided maximum limit of elements.

      If possible, implementations should use smart batching to best handle burst traffic.

      Parameters:
      target - in to which elements are drained.
      limit - maximum number of elements to be drained in a drain operation.
      Returns:
      the number of elements actually drained.
    • sequenceArrayOffset

      private static long sequenceArrayOffset(long sequence, long mask)