Class RingBuffer<E>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.concurrent.RingBuffer<E>
-
- Type Parameters:
E
- the type of elements stored in the buffer
public class RingBuffer<E> extends Object
A striped ring buffer for lock-free access recording in cache operations.This implementation provides high-throughput access recording by using multiple ring buffers (stripes) to reduce contention. Each thread maps to a specific stripe based on its hash, distributing the load evenly.
Key Features:
- Lock-Free Writes: Uses CAS operations for buffer updates
- Striped Architecture: Multiple buffers reduce contention
- Overflow Handling: Graceful degradation when buffers are full
- Memory Efficient: Compact representation of buffer entries
- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RingBuffer.DrainStatus
Drain status for coordinating buffer maintenance.
-
Constructor Summary
Constructors Constructor Description RingBuffer()
Creates a new ring buffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears all elements from the buffer.int
drain(Consumer<E> processor)
Drains elements from the buffer and processes them.RingBuffer.DrainStatus
getDrainStatus()
Gets the current drain status.boolean
isEmpty()
Checks if the buffer is empty.boolean
isFull()
Checks if the buffer is full.boolean
offer(E element)
Attempts to add an element to the buffer.int
size()
Returns the current size of the buffer.
-
-
-
Method Detail
-
offer
public boolean offer(E element)
Attempts to add an element to the buffer.- Parameters:
element
- the element to add- Returns:
- true if the element was added, false if the buffer is full
-
drain
public int drain(Consumer<E> processor)
Drains elements from the buffer and processes them.- Parameters:
processor
- the function to process each element- Returns:
- the number of elements processed
-
size
public int size()
Returns the current size of the buffer.- Returns:
- the number of elements in the buffer
-
isEmpty
public boolean isEmpty()
Checks if the buffer is empty.- Returns:
- true if the buffer is empty
-
isFull
public boolean isFull()
Checks if the buffer is full.- Returns:
- true if the buffer is full
-
getDrainStatus
public RingBuffer.DrainStatus getDrainStatus()
Gets the current drain status.- Returns:
- the drain status
-
clear
public void clear()
Clears all elements from the buffer.
-
-