public class RingBuffer extends Object
PaddedAtomicLong
is using on 'tail' and 'cursor'
A ring buffer is consisted of:
限定符和类型 | 字段和说明 |
---|---|
static int |
DEFAULT_PADDING_PERCENT |
构造器和说明 |
---|
RingBuffer(int bufferSize)
Constructor with buffer size, paddingFactor default as 50
|
RingBuffer(int bufferSize,
int paddingFactor)
Constructor with buffer size & padding factor
|
限定符和类型 | 方法和说明 |
---|---|
protected int |
calSlotIndex(long sequence)
Calculate slot index with the slot sequence (sequence % bufferSize)
|
protected void |
discardPutBuffer(RingBuffer ringBuffer,
long uid)
Discard policy for
RejectedPutBufferHandler , we just do logging |
protected void |
exceptionRejectedTakeBuffer(RingBuffer ringBuffer)
Policy for
RejectedTakeBufferHandler , throws RuntimeException after logging |
int |
getBufferSize() |
long |
getCursor() |
long |
getTail()
Getters
|
boolean |
put(long uid)
Put an UID in the ring & tail moved
We use 'synchronized' to guarantee the UID fill in slot & publish new tail sequence as atomic operations Note that: It is recommended to put UID in a serialize way, cause we once batch generate a series UIDs and put the one by one into the buffer, so it is unnecessary put in multi-threads |
void |
setBufferPaddingExecutor(BufferPaddingExecutor bufferPaddingExecutor)
Setters
|
void |
setRejectedPutHandler(RejectedPutBufferHandler rejectedPutHandler) |
void |
setRejectedTakeHandler(RejectedTakeBufferHandler rejectedTakeHandler) |
long |
take()
Take an UID of the ring at the next cursor, this is a lock free operation by using atomic cursor
Before getting the UID, we also check whether reach the padding threshold,
the padding buffer operation will be triggered in another thread
If there is no more available UID to be taken, the specified RejectedTakeBufferHandler will be applied |
String |
toString() |
public static final int DEFAULT_PADDING_PERCENT
public RingBuffer(int bufferSize)
bufferSize
- must be positive & a power of 2public RingBuffer(int bufferSize, int paddingFactor)
bufferSize
- must be positive & a power of 2paddingFactor
- percent in (0 - 100). When the count of rest available UIDs reach the threshold, it will trigger padding bufferpublic boolean put(long uid)
uid
- RejectedPutBufferHandler
public long take()
Before getting the UID, we also check whether reach the padding threshold,
the padding buffer operation will be triggered in another thread
If there is no more available UID to be taken, the specified RejectedTakeBufferHandler
will be applied
IllegalStateException
- if the cursor moved backprotected int calSlotIndex(long sequence)
protected void discardPutBuffer(RingBuffer ringBuffer, long uid)
RejectedPutBufferHandler
, we just do loggingprotected void exceptionRejectedTakeBuffer(RingBuffer ringBuffer)
RejectedTakeBufferHandler
, throws RuntimeException
after loggingpublic long getTail()
public long getCursor()
public int getBufferSize()
public void setBufferPaddingExecutor(BufferPaddingExecutor bufferPaddingExecutor)
public void setRejectedPutHandler(RejectedPutBufferHandler rejectedPutHandler)
public void setRejectedTakeHandler(RejectedTakeBufferHandler rejectedTakeHandler)