title: Need to understand the problem in AtomicInteger code usage in multithreaded environment
body: <p>In one of the interview, a coding question was asked to me and I had to find the problem in that code and suggest proper solution.</p>
  
  <p>Please find below the entire code:</p>
  
  <pre><code>import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.concurrent.atomic.AtomicInteger;
  
  public class Atomic {
  
      static AtomicInteger count = new AtomicInteger(0);
      static int counter = 0;
  
      public static class Runnable extends Thread {
  
      public void run() {
          while (count.getAndSet(1) != 0) {
              try {
                  Thread.sleep(3000);
              } catch (Exception e) {
  
              }
          }
          counter = counter + 1;
          count.set(0);
      }
  
  }
  
  public static void main(String[] args) {
      ExecutorService executor = Executors.newFixedThreadPool(10);
      for (int i = 0; i &lt; 10; i++) {
          Runnable runnable = new Runnable();
          executor.execute(runnable);
      }
      executor.shutdown();
  }
  
  }
  </code></pre>
  
  <p>This code is running properly. But question is , there is some problem in this code if number of threads get increased or if I run For loop for almost 10000 times.</p>
  
  <p>I tried to find the problem, but couldn't find one.</p>