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 final int
     
    static final int
     
    static final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <E> Buffer<E>
    Returns a no-op implementation.
    void
    drainTo(@NonNull Consumer<E> consumer)
    Drains the buffer, sending each element to the consumer for processing.
    int
    offer(@NonNull E e)
    Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.
    int
    Returns the number of elements that have been read from the buffer.
    default int
    Returns the number of elements residing in the buffer.
    int
    Returns the number of elements that have been written to the buffer.
  • Field Details

  • Method Details

    • 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 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