title: Loop doesn't see value changed by other thread without a print statement
body: <p>In my code I have a loop that waits for some state to be changed from a different thread. The other thread works, but my loop never sees the changed value. <strong>It waits forever.</strong> However, when I put a <code>System.out.println</code> statement in the loop, it suddenly works! Why?</p>
  
  <hr>
  
  <p>The following is an example of my code:</p>
  
  <pre><code>class MyHouse {
      boolean pizzaArrived = false;
  
      void eatPizza() {
          while (pizzaArrived == false) {
              //System.out.println("waiting");
          }
  
          System.out.println("That was delicious!");
      }
  
      void deliverPizza() {
          pizzaArrived = true;
      }
  }
  </code></pre>
  
  <p>While the while loop is running, I call <code>deliverPizza()</code> from a different thread to set the <code>pizzaArrived</code> variable. But the loop only works when I uncomment the <code>System.out.println("waiting");</code> statement. What's going on?</p>