Class RingBufferImpl
java.lang.Object
express.mvp.roray.utils.collections.RingBufferImpl
- All Implemented Interfaces:
RingBuffer, AutoCloseable
An MPMC (Multi-Producer Multi-Consumer) lock-free ring buffer.
Uses a sequence buffer to ensure consistency of slots.
-
Constructor Summary
ConstructorsConstructorDescriptionRingBufferImpl(int capacity) Creates a new ring buffer with the specified capacity. -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()The total capacity of the ring.voidclose()Releases the underlying off-heap memory.booleanisEmpty()booleanisFull()booleanoffer(int item) Offers an item to the ring.intpoll()Polls an item from the ring.intsize()The current number of items in the ring.
-
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:RingBufferOffers an item to the ring.- Specified by:
offerin interfaceRingBuffer- Parameters:
item- The item (index) to enqueue.- Returns:
- true if successful, false if full.
-
poll
public int poll()Description copied from interface:RingBufferPolls an item from the ring.- Specified by:
pollin interfaceRingBuffer- Returns:
- The item, or -1 (or a configured sentinel) if empty.
-
capacity
public int capacity()Description copied from interface:RingBufferThe total capacity of the ring.- Specified by:
capacityin interfaceRingBuffer
-
size
public int size()Description copied from interface:RingBufferThe current number of items in the ring.- Specified by:
sizein interfaceRingBuffer
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceRingBuffer
-
isFull
public boolean isFull()- Specified by:
isFullin interfaceRingBuffer
-
close
public void close()Description copied from interface:RingBufferReleases the underlying off-heap memory.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceRingBuffer
-