Class ManyToOneConcurrentArrayQueue<E>

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

    public class ManyToOneConcurrentArrayQueue<E>
    extends AbstractConcurrentArrayQueue<E>
    Many producer to one consumer concurrent queue that is array backed. The algorithm is a variation of Fast Flow consumer adapted to work with the Java Memory Model on arrays by using Unsafe.

    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.

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

    • Constructor Detail

      • ManyToOneConcurrentArrayQueue

        public ManyToOneConcurrentArrayQueue​(int requestedCapacity)
        Constructs a queue with the requested capacity.
        Parameters:
        requestedCapacity - of the queue.
    • Method Detail

      • offer

        public boolean offer​(E e)
      • poll

        public E poll()
      • drain

        public int drain​(java.util.function.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​(java.util.function.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​(java.util.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.