Package org.jgroups.util
Class RingBuffer<T>
- java.lang.Object
- 
- org.jgroups.util.RingBuffer<T>
 
- 
 public class RingBuffer<T> extends java.lang.ObjectRing 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
- Author:
- Bela Ban
 
- 
- 
Constructor SummaryConstructors Constructor Description RingBuffer(java.lang.Class<T> element_type)RingBuffer(java.lang.Class<T> element_type, int capacity)
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description void_waitForMessages()T[]buf()intcapacity()RingBuffer<T>clear()intcountLockLockless()intdrainTo(java.util.Collection<? super T> c)Removes as many messages as possible and adds them to c.intdrainTo(java.util.Collection<? super T> c, int max_elements)Removes a number of messages and adds them to c.intdrainTo(T[] c)Removes messages and adds them to c.intdrainToBlocking(java.util.Collection<? super T> c)Removes as many messages as possible and adds them to c.intdrainToBlocking(java.util.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()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.intreadIndex()intreadIndexLockless()protected intrealIndex(int index)Apparently much more efficient than mod (%)intsize()Ttake()Removes the next available element, blocking until one is available (if needed).java.lang.StringtoString()intwaitForMessages()Blocks until messages are availableintwaitForMessages(int num_spins, java.util.function.BiConsumer<java.lang.Integer,java.lang.Integer> wait_strategy)Blocks until messages are availableintwriteIndex()
 
- 
- 
- 
Field Detail- 
bufprotected final T[] buf 
 - 
riprotected int ri 
 - 
wiprotected int wi 
 - 
countprotected int count 
 - 
lockprotected final java.util.concurrent.locks.Lock lock 
 - 
not_emptyprotected final java.util.concurrent.locks.Condition not_empty 
 - 
not_fullprotected final java.util.concurrent.locks.Condition not_full 
 
- 
 - 
Method Detail- 
bufpublic T[] buf() 
 - 
capacitypublic int capacity() 
 - 
readIndexLocklesspublic int readIndexLockless() 
 - 
countLockLocklesspublic int countLockLockless() 
 - 
readIndexpublic int readIndex() 
 - 
writeIndexpublic int writeIndex() 
 - 
sizepublic int size() 
 - 
isEmptypublic boolean isEmpty() 
 - 
clearpublic RingBuffer<T> clear() 
 - 
putpublic RingBuffer<T> put(T element) throws java.lang.InterruptedException 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:
- java.lang.InterruptedException
 
 - 
takepublic T take() throws java.lang.InterruptedException Removes the next available element, blocking until one is available (if needed).- Returns:
- The next available element
- Throws:
- java.lang.InterruptedException
 
 - 
drainTopublic int drainTo(java.util.Collection<? super T> c) 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:
- java.lang.NullPointerException- If c is null
 
 - 
drainToBlockingpublic int drainToBlocking(java.util.Collection<? super T> c) throws java.lang.InterruptedException 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:
- java.lang.NullPointerException- If c is null
- java.lang.InterruptedException
 
 - 
drainTopublic int drainTo(java.util.Collection<? super T> c, int max_elements) 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:
- java.lang.NullPointerException- If c is null
 
 - 
drainToBlockingpublic int drainToBlocking(java.util.Collection<? super T> c, int max_elements) throws java.lang.InterruptedException 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:
- java.lang.NullPointerException- If c is null
- java.lang.InterruptedException
 
 - 
drainTopublic int drainTo(T[] c) 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
 
 - 
drainToBlockingpublic int drainToBlocking(T[] c) throws java.lang.InterruptedException 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:
- java.lang.InterruptedException
 
 - 
publishReadIndexpublic RingBuffer<T> publishReadIndex(int num_elements_read) 
 - 
waitForMessagespublic int waitForMessages() throws java.lang.InterruptedExceptionBlocks until messages are available- Throws:
- java.lang.InterruptedException
 
 - 
waitForMessagespublic int waitForMessages(int num_spins, java.util.function.BiConsumer<java.lang.Integer,java.lang.Integer> wait_strategy) throws java.lang.InterruptedExceptionBlocks until messages are available- Parameters:
- num_spins- the number of times we should spin before acquiring a lock
- wait_strategy- the strategy used to spin. The first parameter is the iteration count and the second parameter is the max number of spins
- Throws:
- java.lang.InterruptedException
 
 - 
_waitForMessagespublic void _waitForMessages() throws java.lang.InterruptedException- Throws:
- java.lang.InterruptedException
 
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 - 
realIndexprotected int realIndex(int index) Apparently much more efficient than mod (%)
 
- 
 
-