title: Is Thread.yield guaranteed to flush reads/writes to memory?
body: <p>Lets save I have this code which exhibits stale cache reads by a thread, which prevent it from exiting its while loop.</p>
  <pre><code>class MyRunnable implements Runnable {
      boolean keepGoing = true; // volatile fixes visibility
      @Override public void run() {
          while ( keepGoing ) {
              // synchronized (this) { } // fixes visibility
              // Thread.yield(); // fixes visibility
              System.out.println(); // fixes visibility 
          }
      }
  }
  class Example {
      public static void main(String[] args) throws InterruptedException{
          MyRunnable myRunnable = new MyRunnable();
          new Thread(myRunnable).start();
          Thread.sleep(100);  
          myRunnable.keepGoing = false;
      }
  }
  </code></pre>
  <p>I believe the Java Memory Model guarantees that all writes to a volatile variable synchronize with all subsequent reads from any thread, so that solves the problem.</p>
  <p>If my understanding is correct the code generated by a synchronized block also flushes out all pending reads and writes, which serves as a kind of &quot;memory barrier&quot; and fixes the issue.</p>
  <p>From practice I've seen that inserting <code>yield</code> and <code>println</code> also makes the variable change visible to the thread and it exits correctly. My question is:</p>
  <p>Is yield/println/io serving as a memory barrier guaranteed in some way by the JMM or is it a lucky side effect that cannot be guaranteed to work?</p>
  <hr />
  <p><strong>Edit:</strong> At least some of the assumptions I made in the phrasing of this question were wrong, for example the one concerning synchronized blocks.  I encourage readers of the question to read the corrections in the answers posted below.</p>