public class RingBuffer<T> extends Object
Modifier and Type | Field and Description |
---|---|
protected T[] |
buf |
protected int |
count |
protected Lock |
lock |
protected Condition |
not_empty |
protected Condition |
not_full |
protected int |
ri |
protected int |
wi |
Constructor and Description |
---|
RingBuffer(Class<T> element_type) |
RingBuffer(Class<T> element_type,
int capacity) |
Modifier and Type | Method and Description |
---|---|
void |
_waitForMessages() |
T[] |
buf() |
int |
capacity() |
RingBuffer<T> |
clear() |
int |
countLockLockless() |
int |
drainTo(Collection<? super T> c)
Removes as many messages as possible and adds them to c.
|
int |
drainTo(Collection<? super T> c,
int max_elements)
Removes a number of messages and adds them to c.
|
int |
drainTo(T[] c)
Removes messages and adds them to c.
|
int |
drainToBlocking(Collection<? super T> c)
Removes as many messages as possible and adds them to c.
|
int |
drainToBlocking(Collection<? super T> c,
int max_elements)
Removes a number of messages and adds them to c.
|
int |
drainToBlocking(T[] c)
Removes messages and adds them to c.
|
boolean |
isEmpty() |
RingBuffer<T> |
publishReadIndex(int num_elements_read) |
RingBuffer<T> |
put(T element)
Tries to add a new element at the current write index and advances the write index.
|
int |
readIndex() |
int |
readIndexLockless() |
protected int |
realIndex(int index)
Apparently much more efficient than mod (%)
|
int |
size() |
T |
take()
Removes the next available element, blocking until one is available (if needed).
|
String |
toString() |
int |
waitForMessages()
Blocks until messages are available
|
int |
waitForMessages(int num_spins,
BiConsumer<Integer,Integer> wait_strategy)
Blocks until messages are available
|
int |
writeIndex() |
protected final T[] buf
protected int ri
protected int wi
protected int count
protected final Lock lock
protected final Condition not_empty
protected final Condition not_full
public T[] buf()
public int capacity()
public int readIndexLockless()
public int countLockLockless()
public int readIndex()
public int writeIndex()
public int size()
public boolean isEmpty()
public RingBuffer<T> clear()
public RingBuffer<T> put(T element) throws InterruptedException
element
- the element to be added. Must not be null, or else this operation returns immediatelyInterruptedException
public T take() throws InterruptedException
InterruptedException
public int drainTo(Collection<? super T> c)
BlockingQueue.drainTo(Collection)
.c
- The collection to which to add the removed messages.NullPointerException
- If c is nullpublic int drainToBlocking(Collection<? super T> c) throws InterruptedException
drainTo(Collection)
, this method
blocks until at least one message is available, or the caller thread is interrupted.c
- The collection to which to add the removed messages.NullPointerException
- If c is nullInterruptedException
public int drainTo(Collection<? super T> c, int max_elements)
BlockingQueue.drainTo(Collection,int)
.c
- The collection to which to add the removed messages.max_elements
- The max number of messages to remove. The actual number of messages removed may be smaller
if the buffer has fewer elementsNullPointerException
- If c is nullpublic int drainToBlocking(Collection<? super T> c, int max_elements) throws InterruptedException
drainTo(Collection,int)
, this method
blocks until at least one message is available, or the caller thread is interrupted.c
- The collection to which to add the removed messages.max_elements
- The max number of messages to remove. The actual number of messages removed may be smaller
if the buffer has fewer elementsNullPointerException
- If c is nullInterruptedException
public int drainTo(T[] c)
c
- The array to add messages to.public int drainToBlocking(T[] c) throws InterruptedException
c
- The array to add messages to.drainTo(Object[])
, this method blocks until at least one message is available or the caller thread
is interrupted.InterruptedException
public RingBuffer<T> publishReadIndex(int num_elements_read)
public int waitForMessages() throws InterruptedException
InterruptedException
public int waitForMessages(int num_spins, BiConsumer<Integer,Integer> wait_strategy) throws InterruptedException
num_spins
- the number of times we should spin before acquiring a lockwait_strategy
- the strategy used to spin. The first parameter is the iteration count and the second
parameter is the max number of spinsInterruptedException
public void _waitForMessages() throws InterruptedException
InterruptedException
protected int realIndex(int index)
Copyright © 2018 JBoss, a division of Red Hat. All rights reserved.