Interface MpscIntQueue

  • All Known Implementing Classes:
    MpscIntQueue.MpscAtomicIntegerArrayQueue

    public interface MpscIntQueue
    A multi-producer (concurrent and thread-safe offer and fill), single-consumer (single-threaded poll and drain) queue of primitive integers.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  MpscIntQueue.MpscAtomicIntegerArrayQueue
      This implementation is based on MpscAtomicUnpaddedArrayQueue from JCTools.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      static MpscIntQueue create​(int size, int emptyValue)
      Create a new queue instance of the given size.
      int drain​(int limit, java.util.function.IntConsumer consumer)
      Remove up to the given limit of elements from the queue, and pass them to the consumer in order.
      int fill​(int limit, java.util.function.IntSupplier supplier)
      Add up to the given limit of elements to this queue, from the given supplier.
      boolean isEmpty()
      Query if the queue is empty or not.
      boolean offer​(int value)
      Offer the given value to the queue.
      int poll()
      Remove and return the next value from the queue, or return the "empty" value if the queue is empty.
      int size()
      Query the number of elements currently in the queue.
    • Method Detail

      • create

        static MpscIntQueue create​(int size,
                                   int emptyValue)
        Create a new queue instance of the given size.

        Note: the size of the queue may be rounded up to nearest power-of-2.

        Parameters:
        size - The required fixed size of the queue.
        emptyValue - The special value that the queue should use to signal the "empty" case. This value will be returned from poll() when the queue is empty, and giving this value to offer(int) will cause an exception to be thrown.
        Returns:
        The queue instance.
      • offer

        boolean offer​(int value)
        Offer the given value to the queue. This will throw an exception if the given value is the "empty" value.
        Parameters:
        value - The value to add to the queue.
        Returns:
        true if the value was added to the queue, or false if the value could not be added because the queue is full.
      • poll

        int poll()
        Remove and return the next value from the queue, or return the "empty" value if the queue is empty.
        Returns:
        The next value or the "empty" value.
      • drain

        int drain​(int limit,
                  java.util.function.IntConsumer consumer)
        Remove up to the given limit of elements from the queue, and pass them to the consumer in order.
        Parameters:
        limit - The maximum number of elements to dequeue.
        consumer - The consumer to pass the removed elements to.
        Returns:
        The actual number of elements removed.
      • fill

        int fill​(int limit,
                 java.util.function.IntSupplier supplier)
        Add up to the given limit of elements to this queue, from the given supplier.
        Parameters:
        limit - The maximum number of elements to enqueue.
        supplier - The supplier to obtain the elements from.
        Returns:
        The actual number of elements added.
      • isEmpty

        boolean isEmpty()
        Query if the queue is empty or not.

        This method is inherently racy and the result may be out of date by the time the method returns.

        Returns:
        true if the queue was observed to be empty, otherwise {@code false.
      • size

        int size()
        Query the number of elements currently in the queue.

        This method is inherently racy and the result may be out of date by the time the method returns.

        Returns:
        An estimate of the number of elements observed in the queue.