title: The correct way to test "Volatile" in java
body: <p>I have read the definition of <strong>volatile</strong> in java, And I run a small test project like below :</p>
  <pre><code>public class MainActivity extends AppCompatActivity
  {
  
      static int num = 0;
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          Thread readerThread = new Thread(() -&gt; {
              int temp = 0;
              while (true)
              {
                  if (temp != num) {
                      temp = num;
                      System.out.println(&quot;reader: value of num = &quot; + num);
                  }
              }
          });
  
          Thread writerThread = new Thread(() -&gt; {
              for (int i = 0; i &lt; 5; i++) {
                  num++;
                  System.out.println(&quot;writer: changed value to = &quot; + num);
  
                  //sleep for readerThread to have enough time to read the change of num, since num++ is not atomic
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
              System.exit(0);
          });
  
          readerThread.start();
          writerThread.start();
  
      }
  }
  </code></pre>
  <p>if I run the main code on Eclipse, the log I get something like this:</p>
  <pre><code>writer: changed value to = 1
  reader: value of num = 1
  writer: changed value to = 2
  writer: changed value to = 3
  writer: changed value to = 4
  writer: changed value to = 5
  </code></pre>
  <p>which is, I guess is right, the result means the reader did't get the value from main memory but the cache memory.</p>
  <p>However, if I build the code on real android device by android studio, the log I get is:</p>
  <pre><code>I/System.out: reader: value of num = 1
  I/System.out: writer: changed value to = 1
  I/System.out: reader: value of num = 2
  I/System.out: writer: changed value to = 2
  I/System.out: writer: changed value to = 3
  I/System.out: reader: value of num = 3
  I/System.out: writer: changed value to = 4
  I/System.out: reader: value of num = 4
  I/System.out: writer: changed value to = 5
  I/System.out: reader: value of num = 5
  </code></pre>
  <p>seems like without <strong>volatile</strong>  the reader can still get the value from main memory, why the outcome is different between these two?</p>