public class RingBuffer<T>
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected T[] |
buf |
protected int |
count |
protected java.util.concurrent.locks.Lock |
lock |
protected java.util.concurrent.locks.Condition |
not_empty |
protected java.util.concurrent.locks.Condition |
not_full |
protected int |
ri |
protected int |
wi |
Constructor and Description |
---|
RingBuffer(java.lang.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() |
java.lang.String |
toString() |
int |
waitForMessages()
Blocks until messages are available
|
int |
waitForMessages(int num_spins,
BiConsumer<java.lang.Integer,java.lang.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 java.util.concurrent.locks.Lock lock
protected final java.util.concurrent.locks.Condition not_empty
protected final java.util.concurrent.locks.Condition not_full
public RingBuffer(java.lang.Class<T> element_type, int capacity)
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 java.lang.InterruptedException
element
- the element to be added. Must not be null, or else this operation returns immediately without
adding the null elementjava.lang.InterruptedException
public T take() throws java.lang.InterruptedException
java.lang.InterruptedException
public RingBuffer<T> publishReadIndex(int num_elements_read)
public int waitForMessages() throws java.lang.InterruptedException
java.lang.InterruptedException
public int waitForMessages(int num_spins, BiConsumer<java.lang.Integer,java.lang.Integer> wait_strategy) throws java.lang.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 spinsjava.lang.InterruptedException
public RingBuffer<T> clear()
public int size()
public boolean isEmpty()
public java.lang.String toString()
toString
in class java.lang.Object
protected int realIndex(int index)
Copyright © 1998-2020 Red Hat. All Rights Reserved.