Interface Buffer<E>

  • Type Parameters:
    E - the type of elements maintained by this buffer
    All Known Implementing Classes:
    BoundedBuffer, BoundedBuffer.RingBuffer, DisabledBuffer, StripedBuffer

    interface Buffer<E>
    A multiple-producer / single-consumer buffer that rejects new elements if it is full or fails spuriously due to contention. Unlike a queue and stack, a buffer does not guarantee an ordering of elements in either FIFO or LIFO order.

    Beware that it is the responsibility of the caller to ensure that a consumer has exclusive read access to the buffer. This implementation does not include fail-fast behavior to guard against incorrect consumer usage.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int FAILED  
      static int FULL  
      static int SUCCESS  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      static <E> Buffer<E> disabled()
      Returns a no-op implementation.
      void drainTo​(java.util.function.Consumer<E> consumer)
      Drains the buffer, sending each element to the consumer for processing.
      int offer​(E e)
      Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.
      int reads()
      Returns the number of elements that have been read from the buffer.
      default int size()
      Returns the number of elements residing in the buffer.
      int writes()
      Returns the number of elements that have been written to the buffer.
    • Method Detail

      • disabled

        static <E> Buffer<E> disabled()
        Returns a no-op implementation.
      • offer

        int offer​(@Nonnull
                  E e)
        Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions. The addition is allowed to fail spuriously if multiple threads insert concurrently.
        Parameters:
        e - the element to add
        Returns:
        1 if the buffer is full, -1 if the CAS failed, or 0 if added
      • drainTo

        void drainTo​(@Nonnull
                     java.util.function.Consumer<E> consumer)
        Drains the buffer, sending each element to the consumer for processing. The caller must ensure that a consumer has exclusive read access to the buffer.
        Parameters:
        consumer - the action to perform on each element
      • size

        default int size()
        Returns the number of elements residing in the buffer.
        Returns:
        the number of elements in this buffer
      • reads

        int reads()
        Returns the number of elements that have been read from the buffer.
        Returns:
        the number of elements read from this buffer
      • writes

        int writes()
        Returns the number of elements that have been written to the buffer.
        Returns:
        the number of elements written to this buffer