E
- implementation storing the data for sharing during exchange or parallel coordination of an event.public abstract class RingBuffer<E>
extends java.lang.Object
implements java.util.function.LongSupplier
Modifier and Type | Field and Description |
---|---|
static long |
INITIAL_CURSOR_VALUE
Set to -1 as sequence starting point
|
Constructor and Description |
---|
RingBuffer() |
Modifier and Type | Method and Description |
---|---|
abstract void |
addGatingSequence(Sequence gatingSequence)
Add the specified gating sequence to this instance of the Disruptor.
|
abstract int |
bufferSize() |
static <E> RingBuffer<E> |
createMultiProducer(java.util.function.Supplier<E> factory,
int bufferSize,
WaitStrategy waitStrategy)
Create a new multiple producer RingBuffer with the specified wait strategy.
|
static <E> RingBuffer<E> |
createMultiProducer(java.util.function.Supplier<E> factory,
int bufferSize,
WaitStrategy waitStrategy,
java.lang.Runnable spinObserver)
Create a new multiple producer RingBuffer with the specified wait strategy.
|
static <E> RingBuffer<E> |
createSingleProducer(java.util.function.Supplier<E> factory,
int bufferSize)
Create a new single producer RingBuffer using the default wait strategy
WaitStrategy.busySpin() . |
static <E> RingBuffer<E> |
createSingleProducer(java.util.function.Supplier<E> factory,
int bufferSize,
WaitStrategy waitStrategy)
Create a new single producer RingBuffer with the specified wait strategy.
|
static <E> RingBuffer<E> |
createSingleProducer(java.util.function.Supplier<E> factory,
int bufferSize,
WaitStrategy waitStrategy,
java.lang.Runnable spinObserver)
Create a new single producer RingBuffer with the specified wait strategy.
|
abstract E |
get(long sequence)
Get the event for a given sequence in the RingBuffer.
|
long |
getAsLong() |
abstract long |
getCursor()
Get the current cursor value for the ring buffer.
|
abstract long |
getMinimumGatingSequence()
Get the minimum sequence value from all of the gating sequences added to this ringBuffer.
|
abstract long |
getMinimumGatingSequence(Sequence sequence)
Get the minimum sequence value from all of the gating sequences added to this ringBuffer.
|
static long |
getMinimumSequence(Sequence[] sequences,
long minimum)
Get the minimum sequence from an array of
Sequence s. |
static long |
getMinimumSequence(Sequence excludeSequence,
Sequence[] sequences,
long minimum)
Get the minimum sequence from an array of
Sequence s. |
abstract int |
getPending()
Get the buffered count
|
abstract Sequence |
getSequence()
Get the current cursor value for the ring buffer.
|
Sequence[] |
getSequenceReceivers() |
static boolean |
isAlert(java.lang.Throwable t)
Test if exception is alert
|
static int |
log2(int i)
Calculate the log base 2 of the supplied integer, essentially reports the location of the highest bit.
|
abstract RingBufferReader |
newReader()
Create a new
RingBufferReader to track
which
messages are available to be read
from the ring buffer given a list of sequences to track. |
static Sequence |
newSequence(long init) |
abstract long |
next()
Increment and return the next sequence for the ring buffer.
|
abstract long |
next(int n)
The same functionality as
next() , but allows the caller to claim the next n sequences. |
abstract void |
publish(long sequence)
Publish the specified sequence.
|
abstract void |
publish(long lo,
long hi)
Publish the specified sequences.
|
abstract long |
remainingCapacity()
Get the remaining capacity for this ringBuffer.
|
abstract boolean |
removeGatingSequence(Sequence sequence)
Remove the specified sequence from this ringBuffer.
|
abstract void |
resetTo(long sequence)
Resets the cursor to a specific value.
|
static void |
throwAlert()
Throw a signal singleton exception that can be checked against
isAlert(Throwable) |
java.lang.String |
toString() |
abstract long |
tryNext()
Increment and return the next sequence for the ring buffer.
|
abstract long |
tryNext(int n)
The same functionality as
tryNext() , but allows the caller to attempt to claim the next n
sequences. |
public static final long INITIAL_CURSOR_VALUE
public static <E> RingBuffer<E> createMultiProducer(java.util.function.Supplier<E> factory, int bufferSize, WaitStrategy waitStrategy)
See MultiProducer
.
E
- the element typefactory
- used to create the events within the ring buffer.bufferSize
- number of elements to create within the ring buffer.waitStrategy
- used to determine how to wait for new elements to become available.public static <E> RingBuffer<E> createMultiProducer(java.util.function.Supplier<E> factory, int bufferSize, WaitStrategy waitStrategy, java.lang.Runnable spinObserver)
See MultiProducer
.
E
- the element typefactory
- used to create the events within the ring buffer.bufferSize
- number of elements to create within the ring buffer.waitStrategy
- used to determine how to wait for new elements to become available.spinObserver
- the Runnable to call on a spin loop waitpublic static <E> RingBuffer<E> createSingleProducer(java.util.function.Supplier<E> factory, int bufferSize)
WaitStrategy.busySpin()
.
See MultiProducer
.
E
- the element typefactory
- used to create the events within the ring buffer.bufferSize
- number of elements to create within the ring buffer.public static <E> RingBuffer<E> createSingleProducer(java.util.function.Supplier<E> factory, int bufferSize, WaitStrategy waitStrategy)
See MultiProducer
.
E
- the element typefactory
- used to create the events within the ring buffer.bufferSize
- number of elements to create within the ring buffer.waitStrategy
- used to determine how to wait for new elements to become available.public static <E> RingBuffer<E> createSingleProducer(java.util.function.Supplier<E> factory, int bufferSize, WaitStrategy waitStrategy, java.lang.Runnable spinObserver)
See MultiProducer
.
E
- the element typefactory
- used to create the events within the ring buffer.bufferSize
- number of elements to create within the ring buffer.waitStrategy
- used to determine how to wait for new elements to become available.spinObserver
- called each time the next claim is spinning and waiting for a slotpublic static long getMinimumSequence(Sequence[] sequences, long minimum)
Sequence
s.sequences
- to compare.minimum
- an initial default minimum. If the array is empty this value will be returned.public static long getMinimumSequence(Sequence excludeSequence, Sequence[] sequences, long minimum)
Sequence
s.excludeSequence
- to exclude from search.sequences
- to compare.minimum
- an initial default minimum. If the array is empty this value will be returned.public static boolean isAlert(java.lang.Throwable t)
t
- exception checkedpublic static int log2(int i)
i
- Value to calculate log2 for.public static Sequence newSequence(long init)
init
- the initial valuepublic static void throwAlert()
isAlert(Throwable)
public abstract void addGatingSequence(Sequence gatingSequence)
gatingSequence
- The sequences to add.public abstract int bufferSize()
public abstract E get(long sequence)
Get the event for a given sequence in the RingBuffer.
This call has 2 uses. Firstly use this call when publishing to a ring buffer. After calling next()
use this call to get hold of the preallocated event to fill with data before calling publish(long)
.
Secondly use this call when consuming data from the ring buffer. After calling RingBufferReader.waitFor(long)
call this method with any value greater than that your current consumer sequence
and less than or equal to the value returned from the RingBufferReader.waitFor(long)
method.
sequence
- for the eventpublic long getAsLong()
getAsLong
in interface java.util.function.LongSupplier
public abstract long getCursor()
RingBufferProducer
that is being used.
See MultiProducer
.
See SingleProducerSequencer
public abstract long getMinimumGatingSequence()
public abstract long getMinimumGatingSequence(Sequence sequence)
sequence
- the target sequencepublic abstract int getPending()
public abstract Sequence getSequence()
RingBufferProducer
that is being used.
See MultiProducer
.
See SingleProducerSequencer
.
public Sequence[] getSequenceReceivers()
public abstract RingBufferReader newReader()
RingBufferReader
to track
which
messages are available to be read
from the ring buffer given a list of sequences to track.RingBufferReader
public abstract long next()
long sequence = ringBuffer.next(); try { Event e = ringBuffer.get(sequence); // Do some work with the event. } finally { ringBuffer.publish(sequence); }
publish(long)
,
get(long)
public abstract long next(int n)
next()
, but allows the caller to claim the next n sequences.
See RingBufferProducer.next(int)
n
- number of slots to claimpublic abstract void publish(long sequence)
sequence
- the sequence to publish.public abstract void publish(long lo, long hi)
See RingBufferProducer.next(int)
lo
- the lowest sequence number to be publishedhi
- the highest sequence number to be publishedpublic abstract long remainingCapacity()
public abstract boolean removeGatingSequence(Sequence sequence)
sequence
- to be removed.public abstract void resetTo(long sequence)
sequence
- The sequence to reset too.java.lang.IllegalStateException
- If any gating sequences have already been specified.public java.lang.String toString()
toString
in class java.lang.Object
public abstract long tryNext() throws reactor.util.concurrent.RingBuffer.InsufficientCapacityException
Increment and return the next sequence for the ring buffer. Calls of this method should ensure that they always publish the sequence afterward. E.g.
long sequence = ringBuffer.next(); try { Event e = ringBuffer.get(sequence); // Do some work with the event. } finally { ringBuffer.publish(sequence); }
This method will not block if there is not space available in the ring buffer, instead it will throw a RuntimeException
.
InsufficientCapacityException
- if the necessary space in the ring buffer is not availablereactor.util.concurrent.RingBuffer.InsufficientCapacityException
publish(long)
,
get(long)
public abstract long tryNext(int n) throws reactor.util.concurrent.RingBuffer.InsufficientCapacityException
tryNext()
, but allows the caller to attempt to claim the next n
sequences.n
- number of slots to claimInsufficientCapacityException
- if the necessary space in the ring buffer is not availablereactor.util.concurrent.RingBuffer.InsufficientCapacityException