title: AtomicInteger Vs synchronized int variable in Java : performance difference
body: <p>After going through the following question
  <a href="https://stackoverflow.com/questions/11551963/can-synchronized-blocks-be-faster-than-atomics">Can synchronized blocks be faster than Atomics?</a>,<br>
  I wrote a simple program to compare the performance difference of <em>AtomicInteger</em> and synchronized block (incrementing int inside it). Every time I run this program it gives me ratio > 100.
  My laptop has Intel Corei3 and following line prints 4.</p>
  
  <pre><code>    System.out.println(Runtime.getRuntime().availableProcessors())
  </code></pre>
  
  <p>When I use</p>
  
  <pre><code>    THREAD_COUNT = 1;
  </code></pre>
  
  <p>the ratio comes minimum. It varies around 100. For</p>
  
  <pre><code>    THREAD_COUNT = 10;  
  </code></pre>
  
  <p>ratio is around 800.</p>
  
  <p><strong>Question 1</strong> : Can you please tell me is this the correct way to test the performance difference of <em>AtomicInteger</em> VS synchronized increment() method?</p>
  
  <p><strong>Question 2</strong> : If I increase <code>THREAD_COUNT</code>, ratio increases, why? I think it is because more threads get blocked at synchronized statement and requires more task for CPU. Please comment.</p>
  
  <pre><code>package concurrent.atomic;
  
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.atomic.AtomicInteger;
  
  public class Performance {
  
      private static final AtomicInteger atomicInt = new AtomicInteger(0);
      private static volatile int counter = 0;
      private static final Object LOCK = new Object();
      private static final int THREAD_COUNT = 10;
  
      public static void main(String[] args) {
  
          System.out.println(Runtime.getRuntime().availableProcessors()); 
  
          Runnable atomic = new Runnable() {
              @Override
              public void run() {
                  while (!Thread.currentThread().isInterrupted()) {
                      int value = atomicInt.incrementAndGet();
                      //if (value % 1000 == 0)
                          //System.out.println("atomic value : "+value);
                  }   
              }
          };
          //System.out.println("1");
          Runnable intCounter = new Runnable() {
              @Override
              public void run() {
                  while (!Thread.currentThread().isInterrupted()) {
                      synchronized (LOCK) {
                          int value = ++counter;
                          //if (value % 1000 == 0)
                              //System.out.println("sync value "+value);
                      }
                  }   
              }
          };
  
          final ExecutorService atomicExecutor = Executors.newCachedThreadPool();
          final ExecutorService primitiveExecutor = Executors.newCachedThreadPool();
          for (int i = 0; i &lt; THREAD_COUNT ; ++i) {
              atomicExecutor.submit(atomic);
              primitiveExecutor.submit(intCounter);
          }
  
          while (true) {
              float ratio = (((float) (atomicInt.get() * 1.0) ) / counter) * 100;
              System.out.println("ratio : " + ratio);
          }
      }
  }
  </code></pre>