In this tutorial, we’ll learn how to implement a Ring Buffer in Java.
Ring Buffer (or Circular Buffer) is a bounded circular data structure that is used for buffering data between two or more threads. As we keep writing to a ring buffer, it wraps around as it reaches the end.
A Ring Buffer is implemented using a fixed-size array that wraps around at the boundaries.
Apart from the array, it keeps track of three things:
The mechanics of how a ring buffer handles these requirements vary with the implementation. For instance, the Wikipedia entry on the subject shows a method using four-pointers.
We’ll borrow the approach from Disruptor‘s implementation of the ring buffer using sequences.
The first thing we need to know is the capacity – the fixed maximum size of the buffer. Next, we’ll use two monotonically increasing sequences:
We can map a sequence to an index in the array by using a mod operation:
1
arrayIndex = sequence % capacity
The mod operation wraps the sequence around the boundaries to derive a slot in the buffer:
Let’s see how we’d insert an element:
1
buffer[++writeSequence % capacity] = element
We are pre-incrementing the sequence before inserting an element.
To consume an element we do a post-increment:
1
element = buffer[readSequence++ % capacity]
In this case, we perform a post-increment on the sequence. Consuming an element doesn’t remove it from the buffer – it just stays in the array until it’s overwritten.
#algorithms #java