title: When does Thread read a non-volatile variable from main memory instead of working memory?
body: <pre><code>public class Main {
  
  public static void main(String[] args) throws InterruptedException {
      Worker w = new Worker();
      w.start();
  
      sleepQuietly(1000);
  
      w.alive = false;
      w.join();
  }
  
  static class Worker extends Thread {
      boolean alive = true;
  
      @Override
      public void run() {
          while (alive) {
          }
      }
  }
  
  static void sleepQuietly(long millis) {
      try {
          Thread.sleep(millis);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>If I run the main method above, the main thread won't stop.But if I run the main method below, the main method will stop.</p>
  
  <pre><code>public class Main {
  
  public static void main(String[] args) throws InterruptedException {
      Worker w = new Worker();
      w.start();
  
      sleepQuietly(1000);
  
      w.alive = false;
      w.join();
  }
  
  static class Worker extends Thread {
      boolean alive = true;
  
      @Override
      public void run() {
          while (alive) {
              sleepQuietly(1); 
          }
      }
  }
  
  static void sleepQuietly(long millis) {
      try {
          Thread.sleep(millis);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>"The memory model guarantees that, given the eventual occurrence of the above operations, a particular update to a particular field made by one thread will eventually be visible to another. But eventually can be an arbitrarily long time. Long stretches of code in threads that use no synchronization can be hopelessly out of synch with other threads with respect to values of fields. In particular, it is always wrong to write loops waiting for values written by other threads unless the fields are volatile or accessed via synchronization"</p>
  
  <p>I read above in <a href="http://gee.cs.oswego.edu/dl/cpj/jmm.html" rel="nofollow noreferrer">http://gee.cs.oswego.edu/dl/cpj/jmm.html</a>, and here is my question:
  1. In the first example, why worker thread can't see the value updated by main thread as the JMM guarantees that a particular update to a particular field made by one thread will eventually be visible to another</p>
  
  <ol start="2">
  <li>Why does Thread.sleep() makes difference?</li>
  </ol>