Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Before I answer your question I want to address </p> <blockquote> <p>BTW, that code terminates without volatile on JRE7</p> </blockquote> <p>This can change if you were to deploy the same application with different runtime arguments. Hoisting isn't necessarily a default implementation for JVMs so it can work in one and not in another.</p> <p>To answer your question there is nothing preventing the Java compiler from executing your latter example like so</p> <pre><code>@Override public void run() { if(b.getField()){ while(true) { System.err.println("Waiting..."); try { Thread.sleep(1000); } catch(InterruptedException ie) { return; } } } } </code></pre> <p>It is still <strong>sequentially consistent</strong> and thus maintains Java's guarantees - you can read specifically <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html" rel="nofollow">17.4.3</a>:</p> <blockquote> <p>Among all the inter-thread actions performed by each thread t, the program order of t is a total order that reflects the order in which these actions would be performed according to the intra-thread semantics of t.</p> <p>A set of actions is sequentially consistent if all actions occur in a total order (the execution order) that is consistent with program order, and furthermore, each read r of a variable v sees the value written by the write w to v such that:</p> </blockquote> <p>In other words - So long as a thread will see the read and write of a field in the same order regardless of the compiler/memory re ordering it is considered sequentially consistent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload