title: Apache Commons - CircularFifoBuffer BufferOverflowException
body: <h1><strong>Background</strong></h1>
  
  <p>I am attempting to use the <code>CircularFifoBuffer</code> class from the Apache Commons library that contains a collection of recent messages received over a WebSocket connection. However, when I reach the size limit of the <code>CircularFifoBuffer</code> in a Linux deployment a <code>BufferOverflowException</code> is being thrown.</p>
  
  <p>I am still fairly new to Java but here's how I'm defining the <code>CircularFifoBuffer</code> instance variable:</p>
  
  <pre><code>private static Buffer&lt;String&gt; recentWebSocketMessages = new CircularFifoBuffer&lt;String&gt;(1000);
  </code></pre>
  
  <p>And here's the exception:</p>
  
  <pre><code>org.apache.commons.collections15.BufferOverflowException: The buffer cannot hold more than 1000 objects.
   at org.apache.commons.collections15.buffer.BoundedFifoBuffer.add(BoundedFifoBuffer.java:218)
   at org.apache.commons.collections15.buffer.CircularFifoBuffer.add(CircularFifoBuffer.java:94)
   at com.example.SystemMonitor.putRecentWebSocketMessage(SystemMonitor.java:228) 
  </code></pre>
  
  <p>What's strange to me is this problem is intermittent, when I write an infinite loop:</p>
  
  <pre><code>while(true)
  {
      recentWebSocketMessages.add("TESTING");
  }
  </code></pre>
  
  <p>The exception doesn't occur (at least in Windows - I haven't been able to test Linux yet)</p>
  
  <h1><strong>Question</strong></h1>
  
  <p>So I guess the main issue here is why is this problem intermittent and can I solve this by defining the static variable as such instead? (Even though this tightly couples <code>recentWebSocketMessages</code> with the <code>CircularFifoBuffer</code> implementation of <code>BoundedFifoBuffer</code>)</p>
  
  <pre><code>private static CircularFifoBuffer&lt;String&gt; recentWebSocketMessages = new CircularFifoBuffer&lt;String&gt;(1000);
  </code></pre>
  
  <h1>Edit</h1>
  
  <p>Thank you sorifiend and Tom for pointing out the simple fact that this implementation is not synchronized. For those interested, this simple thread example below proves that multi-threading is the issue and multiple threads accessing the buffer simultaneously can/will cause the <code>BufferOverflowException</code> to occur (with my original definition of the buffer):</p>
  
  <pre><code>while(true)
  {
      new Thread(new Runnable() 
      {
          @Override
          public void run()
          {
              try 
              {
                  recentWebSocketMessages.add("TESTING");
              } 
              catch(BufferOverflowException e) 
              {
                  e.printStackTrace();
              }
          }
      }).start();
  }
  </code></pre>
  
  <p>The same code with the following definition of <code>recentWebSocketMessages</code> does not throw the <code>BufferOverflowException</code> since accesses to the buffer are synchronized as mentioned in the API and in sorifiend's answer:</p>
  
  <pre><code>Buffer recentWebSocketMessages = BufferUtils.synchronizedBuffer(new CircularFifoBuffer(1000));
  </code></pre>