Package org.jgroups.util
Class RingBuffer<T>
java.lang.Object
org.jgroups.util.RingBuffer<T>
Ring buffer of fixed capacity designed for multiple writers but only a single reader. Advancing the read or
write index blocks until it is possible to do so.
- Since:
- 4.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRingBuffer(Class<T> element_type) RingBuffer(Class<T> element_type, int capacity) -
Method Summary
Modifier and TypeMethodDescriptionvoidT[]buf()intcapacity()clear()intintdrainTo(Collection<? super T> c) Removes as many messages as possible and adds them to c.intdrainTo(Collection<? super T> c, int max_elements) Removes a number of messages and adds them to c.intRemoves messages and adds them to c.intdrainToBlocking(Collection<? super T> c) Removes as many messages as possible and adds them to c.intdrainToBlocking(Collection<? super T> c, int max_elements) Removes a number of messages and adds them to c.intdrainToBlocking(T[] c) Removes messages and adds them to c.booleanisEmpty()publishReadIndex(int num_elements_read) Tries to add a new element at the current write index and advances the write index.intintprotected intrealIndex(int index) Apparently much more efficient than mod (%)intsize()take()Removes the next available element, blocking until one is available (if needed).toString()intBlocks until messages are availableintwaitForMessages(int num_spins, BiConsumer<Integer, Integer> wait_strategy) Blocks until messages are availableint
-
Field Details
-
buf
-
ri
protected int ri -
wi
protected int wi -
count
protected int count -
lock
-
not_empty
-
not_full
-
-
Constructor Details
-
RingBuffer
-
RingBuffer
-
-
Method Details
-
buf
-
capacity
public int capacity() -
readIndexLockless
public int readIndexLockless() -
countLockLockless
public int countLockLockless() -
readIndex
public int readIndex() -
writeIndex
public int writeIndex() -
size
public int size() -
isEmpty
public boolean isEmpty() -
clear
-
put
Tries to add a new element at the current write index and advances the write index. If the write index is at the same position as the read index, this will block until the read index is advanced.- Parameters:
element- the element to be added. Must not be null, or else this operation returns immediately- Throws:
InterruptedException
-
take
Removes the next available element, blocking until one is available (if needed).- Returns:
- The next available element
- Throws:
InterruptedException
-
drainTo
Removes as many messages as possible and adds them to c. Same semantics asBlockingQueue.drainTo(Collection).- Parameters:
c- The collection to which to add the removed messages.- Returns:
- The number of messages removed
- Throws:
NullPointerException- If c is null
-
drainToBlocking
Removes as many messages as possible and adds them to c. Contrary todrainTo(Collection), this method blocks until at least one message is available, or the caller thread is interrupted.- Parameters:
c- The collection to which to add the removed messages.- Returns:
- The number of messages removed
- Throws:
NullPointerException- If c is nullInterruptedException
-
drainTo
Removes a number of messages and adds them to c. Same semantics asBlockingQueue.drainTo(Collection,int).- Parameters:
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 elements- Returns:
- The number of messages removed
- Throws:
NullPointerException- If c is null
-
drainToBlocking
Removes a number of messages and adds them to c. Contrary todrainTo(Collection,int), this method blocks until at least one message is available, or the caller thread is interrupted.- Parameters:
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 elements- Returns:
- The number of messages removed
- Throws:
NullPointerException- If c is nullInterruptedException
-
drainTo
Removes messages and adds them to c.- Parameters:
c- The array to add messages to.- Returns:
- The number of messages removed and added to c. This is min(count, c.length). If no messages are present, this method returns immediately
-
drainToBlocking
Removes messages and adds them to c.- Parameters:
c- The array to add messages to.- Returns:
- The number of messages removed and added to c. This is min(count, c.length). Contrary to
drainTo(Object[]), this method blocks until at least one message is available or the caller thread is interrupted. - Throws:
InterruptedException
-
publishReadIndex
-
waitForMessages
Blocks until messages are available- Throws:
InterruptedException
-
waitForMessages
public int waitForMessages(int num_spins, BiConsumer<Integer, Integer> wait_strategy) throws InterruptedExceptionBlocks until messages are available- Parameters:
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 spins- Throws:
InterruptedException
-
_waitForMessages
- Throws:
InterruptedException
-
toString
-
realIndex
protected int realIndex(int index) Apparently much more efficient than mod (%)
-