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,
int capacity) |
Modifier and Type | Method and Description |
---|---|
T[] |
buf() |
int |
capacity() |
RingBuffer<T> |
clear() |
int |
count() |
int |
countLockLockless() |
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() |
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 count()
public RingBuffer<T> put(T element) throws InterruptedException
element
- the element to be added. Must not be null, or else this operation returns immediately without
adding the null elementInterruptedException
public T take() throws InterruptedException
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 RingBuffer<T> clear()
public int size()
public boolean isEmpty()
protected int realIndex(int index)
Copyright © 2018 JBoss, a division of Red Hat. All rights reserved.