title: Does the volatile keyword guarantee the latest value to be read by a reader thread? If not, what is its use?
body: <p>Please refer to the following codes.</p>
  <p>Shared.java</p>
  <pre><code>class Shared
  {
      public volatile int i = 0;
  }
  </code></pre>
  <p>CreateThread.java</p>
  <pre><code>class CreateThread
  {
      public static void main(String[] args) throws InterruptedException
      {
          Shared s = new Shared();
          MyThread t1 = new MyThread(s);
          t1.start();
          while(s.i!=7)
              System.out.println(s.i);
      }
  }
  </code></pre>
  <p>MyThread.java</p>
  <pre><code>class MyThread extends Thread
  {
      Shared s = null;
      MyThread(Shared s)
      {
          this.s = s;
      }   
      public void run()
      {
          for(int j=0; j&lt;15; j++)
          {
      /*      try
              {
                  Thread.sleep(1000);
              }
              catch(Exception e){}*/
              s.i = s.i+1;
          //  System.out.println(&quot;New Value of s.i &quot;+s.i);
      /*      try
              {
                  Thread.sleep(1000);
              }
              catch(Exception e){}*/
          }
          System.out.println(&quot;Thread Going to Stop&quot;);
      }
  }
  </code></pre>
  <p>If the new thread is not allowed to sleep, then it seems that the main thread cannot find all the values of the variable s.i. Because in this case, we are getting the following output.</p>
  <pre><code>0
  15
  15
  15
  ..
  ..
  ..
  </code></pre>
  <p>If the new thread is allowed to sleep, then it seems that the main thread can find all the values of the variable s.i. Because in this case, we are getting the following output.</p>
  <pre><code>0
  0
  ..
  1
  1
  ..
  2
  2
  ..
  3
  3
  ..
  4
  4
  ..
  upto 6
  </code></pre>
  <p>From the above output, it is clear that if the new thread does not go into the sleep state, the new thread is changing the value of s.i in memory several times before the thread main gets a chance to read it.</p>
  <p>If I change the program as:</p>
  <p>Shared.java</p>
  <pre><code>class Shared
  {
      public /*volatile*/ boolean i = false;
  }
  </code></pre>
  <p>MyThread.java</p>
  <pre><code>class MyThread extends Thread
  {
      Shared s = null;
      MyThread(Shared s)
      {
          this.s = s;
      }   
      public void run()
      {
      
              s.i = true;
              System.out.println(&quot;New Value of s.i &quot;+s.i);
              try
              {
                  Thread.sleep(10000);
              }
              catch(Exception e){}
          System.out.println(&quot;Thread Going to Stop&quot;);
      }
  }
  </code></pre>
  <p>CreateThread.java</p>
  <pre><code>class CreateThread
  {
      public static void main(String[] args) throws InterruptedException
      {
          Shared s = new Shared();
          MyThread t1 = new MyThread(s);
          t1.start();
          while(s.i==false)
              System.out.println(s.i);
          System.out.println(s.i+&quot; Main Stopped&quot;);
      }
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>C:\Users\gyan0\OneDrive\Desktop&gt;java CreateThread
  false
  New Value of s.i true
  true Main Stopped
  Thread Going to Stop
  </code></pre>
  <p>It seems that the data, even though it is not volatile, is becoming immediately available to the thread.</p>
  <p>Following are my questions.</p>
  <ol>
  <li>Does the volatile keyword guarantee the latest value to be read by a reader thread?</li>
  <li>What the benefits we would get if volatile keyword saves a data in memory by not saving it in CPU Register? Can you give a practical working example proving the benefits of the volatile?</li>
  </ol>