Class RingBuffer<T>

java.lang.Object
org.jgroups.util.RingBuffer<T>

public class RingBuffer<T> extends Object
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 Details

    • buf

      protected final T[] buf
    • ri

      protected int ri
    • wi

      protected int wi
    • count

      protected int count
    • lock

      protected final Lock lock
    • not_empty

      protected final Condition not_empty
    • not_full

      protected final Condition not_full
  • Constructor Details

    • RingBuffer

      public RingBuffer(Class<T> element_type)
    • RingBuffer

      public RingBuffer(Class<T> element_type, int capacity)
  • Method Details

    • buf

      public T[] 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

      public RingBuffer<T> clear()
    • put

      public RingBuffer<T> put(T element) throws 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:
      InterruptedException
    • take

      public T take() throws InterruptedException
      Removes the next available element, blocking until one is available (if needed).
      Returns:
      The next available element
      Throws:
      InterruptedException
    • drainTo

      public int drainTo(Collection<? super T> c)
      Removes as many messages as possible and adds them to c. Same semantics as BlockingQueue.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

      public int drainToBlocking(Collection<? super T> c) throws InterruptedException
      Removes as many messages as possible and adds them to c. Contrary to drainTo(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 null
      InterruptedException
    • drainTo

      public int drainTo(Collection<? super T> c, int max_elements)
      Removes a number of messages and adds them to c. Same semantics as BlockingQueue.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

      public int drainToBlocking(Collection<? super T> c, int max_elements) throws InterruptedException
      Removes a number of messages and adds them to c. Contrary to drainTo(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 null
      InterruptedException
    • drainTo

      public 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
    • drainToBlocking

      public int drainToBlocking(T[] c) throws 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:
      InterruptedException
    • publishReadIndex

      public RingBuffer<T> publishReadIndex(int num_elements_read)
    • waitForMessages

      public int waitForMessages() throws InterruptedException
      Blocks until messages are available
      Throws:
      InterruptedException
    • waitForMessages

      public int waitForMessages(int num_spins, BiConsumer<Integer,Integer> wait_strategy) throws InterruptedException
      Blocks 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:
      InterruptedException
    • _waitForMessages

      public void _waitForMessages() throws InterruptedException
      Throws:
      InterruptedException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • realIndex

      protected int realIndex(int index)
      Apparently much more efficient than mod (%)