title: Are primitive datatypes thread-safe in Java
body: <p>Are the primitive data types like <code>int</code> &amp; <code>short</code> thread-safe in Java? I have executed the following code and couldn't see expected result 500 some times.</p>
  
  <pre><code>public class SampleThree extends Thread
  {
      static long wakeUpTime = System.currentTimeMillis() + (1000*20);
      static int inT;
      public static void main(String args[])
      {
          System.out.println("initial:" + inT);
          for(int i=0; i&lt;500; i++)
              new SampleThree().start();
          try {
              Thread.sleep(wakeUpTime - System.currentTimeMillis() + (1000*30));
              System.out.println("o/p:" + inT);
          }
          catch(Exception e){
              e.printStackTrace();
          }
      }
  
      public void run()
      {
          try {
              long s = wakeUpTime - System.currentTimeMillis();
              System.out.println("will sleep ms: " + s);
              Thread.sleep(s);
              inT++; // System.out.println(inT);
          }
          catch(Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Here concurrently 500 thread will update the int variable <code>inT</code>. Main thread after waiting for concurrent update to be completed, prints <code>inT</code> value.</p>
  
  <p>Find similar example <a href="http://2dayit.blogspot.in/2014/09/are-primitive-datatypes-in-java-thread.html">here</a></p>