title: Performance of put() in LinkedBlockingQueue
body: <p>I am working on a stock trading application whose <em>key</em> feature is to accept some data from another system as fast as possible ( without blocking ). My application will then process the data at a later time.</p>
  
  <p>So what I though was to let the messages queue in LinkedBlockingQueue/LinkedTransferQueue and then periodically drain the queue and process the data in a background thread.</p>
  
  <p>So something along the lines of:</p>
  
  <pre><code>    private final LinkedTransferQueue&lt;Data&gt; queue = new LinkedTransferQueue&lt;Data&gt;();
  
      public void store( int index, long time, String[] data ) throws InterruptedException{
         Data data = new Data( index, time, data );
         queue.put( data );
      }
  
      private class BackgroundProcessor implements Runnable{
  
      private List&lt;Data&gt; entryList = new LinkedList&lt;Data&gt;( );
  
      @Override
      public void run(){
  
          try {
              while ( keepProcessing ){
  
                  int count = queue.drainTo( entryList );
  
                  for ( Data data : entryList ){
                  //process data
                  }
              }
          } catch( Exception e ){
                 logger.error("Exception while processing data.", e);
          } 
      }
  
  }
  </code></pre>
  
  <p>I then wanted to test the performance of this approach:</p>
  
  <pre><code>    public void testStore( String[] dataArray ) throws InterruptedException{
  
          int size = 100 * 1000;
  
          long iTime = System.nanoTime();
          for ( int i=0; i &lt; size; i++ ){
             store( i, System.nanoTime, dataArray );
          }
          long fTime = System.nanoTime();
  
          System.err.println("Average Time (nanos): " +   (fTime - iTime)/size;
  
          float avgTimeInMicros = ((float) (fTime - iTime)/(size * 1000));
          System.err.println("Average Time (micros): " + avgTimeInMicros);
      }
  </code></pre>
  
  <p>I see that in my testStore(), if size = 100,0000, I can create the Data object ( which is an immutable object) and enqueue in 0.8 micro-second. However, if I decrease the size to 50, it takes as much as 20 micros.</p>
  
  <p>I am assuming, the jvm after a while optimizes my code.
  However, in my application, getting 50 data messages in a burst is more realistic, is there a way to tune the jvm ( or my code ) to enqueue in 1-2 micros regardless of the burst size?</p>
  
  <p>P.S I tried this test on jdk 1.6 with -mx == -ms 512m.</p>