Class RingBufferImpl

java.lang.Object
express.mvp.roray.utils.collections.RingBufferImpl
All Implemented Interfaces:
RingBuffer, AutoCloseable

public class RingBufferImpl extends Object implements RingBuffer
An MPMC (Multi-Producer Multi-Consumer) lock-free ring buffer.

Uses a sequence buffer to ensure consistency of slots.

  • Constructor Summary

    Constructors
    Constructor
    Description
    RingBufferImpl(int capacity)
    Creates a new ring buffer with the specified capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    The total capacity of the ring.
    void
    Releases the underlying off-heap memory.
    boolean
     
    boolean
     
    boolean
    offer(int item)
    Offers an item to the ring.
    int
    Polls an item from the ring.
    int
    The current number of items in the ring.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RingBufferImpl

      public RingBufferImpl(int capacity)
      Creates a new ring buffer with the specified capacity.
      Parameters:
      capacity - the capacity (must be a power of 2)
  • Method Details

    • offer

      public boolean offer(int item)
      Description copied from interface: RingBuffer
      Offers an item to the ring.
      Specified by:
      offer in interface RingBuffer
      Parameters:
      item - The item (index) to enqueue.
      Returns:
      true if successful, false if full.
    • poll

      public int poll()
      Description copied from interface: RingBuffer
      Polls an item from the ring.
      Specified by:
      poll in interface RingBuffer
      Returns:
      The item, or -1 (or a configured sentinel) if empty.
    • capacity

      public int capacity()
      Description copied from interface: RingBuffer
      The total capacity of the ring.
      Specified by:
      capacity in interface RingBuffer
    • size

      public int size()
      Description copied from interface: RingBuffer
      The current number of items in the ring.
      Specified by:
      size in interface RingBuffer
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface RingBuffer
    • isFull

      public boolean isFull()
      Specified by:
      isFull in interface RingBuffer
    • close

      public void close()
      Description copied from interface: RingBuffer
      Releases the underlying off-heap memory.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface RingBuffer