title: Volatile AtomicInteger not working
body: <p>Hi I was trying volatile. I create 10 threads from my main Thread and I print value of static counter from each thread.</p>
  
  <p>The output is uncertain. Can anyone please let me know why its not working.</p>
  
  <pre><code>public class Main {
      static AtomicInteger  counter = new AtomicInteger(0);
  
      public static void main(String[] args) {
          while(counter.getAndIncrement() &lt; 10){
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          System.out.println(counter.get());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }).start();
          }
      }
  }
  </code></pre>
  
  <p>In this I also tried changing the counter as</p>
  
  <pre><code>static volatile int counter = 0;
  </code></pre>
  
  <p>The out put I get is </p>
  
  <blockquote>
    <p>3 3 6 6 7 7 10 10 11 11</p>
  </blockquote>
  
  <p>The output is different every-time.
  I don't expect them in proper order but I expect unique values from 0 - 10.</p>