Difference between BoundedFifoBuffer and CircularFifoBuffer?

In apache shared collections , what is the difference between:

  • CircularFifoBuffer
  • BoundedFifoBuffer

Ok, the first one deletes the old records when it is full, and the other one deletes the records in the same order as the one who came. But isn't it?

+3


source to share


3 answers


CircularFifoBuffer

extends BoundedFifoBuffer

. It only overrides the single method - add

:

public boolean add(Object element) {
    if (isFull()) {
        remove();
    }
    return super.add(element);
}

      



So the only difference is that it BoundedFifoBuffer

throws an exception when it is full and you try to add a new element, but it CircularFifoBuffer

removes the oldest element.

+5


source


let's say you put n elements in the buffer at index 1,2,3 - n

Now both CircularFifoBuffer and BoundedFifoBuffer are full.



In a bounded buffer, since the nth element is full, it will say the entire buffer is full.

In a CircularFifoBuffer, if the buffer is full, then the last added element is discarded so that a new element can be inserted. So if the nth element is full, it will put the next element at the 1st index.

+3


source


When the BoundedFifoBuffer function is full, it prevents another element from being inserted . But in the CircularFifoBuffer, it removes the older one when it is full.

+2


source







All Articles