Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is sort of a side note explanation on some of the details of volatile. Writing this here because it is too much for an comment. I want to give some examples which show how volatile affects visibility, and how that changed in jdk 1.5.</p> <p>Given the following example code:</p> <pre><code>public class MyClass { private int _n; private volatile int _volN; public void setN(int i) { _n = i; } public void setVolN(int i) { _volN = i; } public int getN() { return _n; } public int getVolN() { return _volN; } public static void main() { final MyClass mc = new MyClass(); Thread t1 = new Thread() { public void run() { mc.setN(5); mc.setVolN(5); } }; Thread t2 = new Thread() { public void run() { int volN = mc.getVolN(); int n = mc.getN(); System.out.println("Read: " + volN + ", " + n); } }; t1.start(); t2.start(); } } </code></pre> <p>The behavior of this test code is well defined in jdk1.5+, but is <strong>not</strong> well defined pre-jdk1.5.</p> <p>In the pre-jdk1.5 world, there was no defined relationship between volatile accesses and non-volatile accesses. therefore, the output of this program could be:</p> <ol> <li>Read: 0, 0</li> <li>Read: 0, 5</li> <li>Read: 5, 0</li> <li>Read: 5, 5</li> </ol> <p>In the jdk1.5+ world, the semantics of volatile were changed so that volatile accesses affect non-volatile accesses in exactly the same way as synchronization. therefore, only certain outputs are possible in the jdk1.5+ world:</p> <ol> <li>Read: 0, 0</li> <li>Read: 0, 5</li> <li>Read: 5, 0 <strong>&lt;- not possible</strong></li> <li>Read: 5, 5</li> </ol> <p>Output 3. is not possible because the reading of "5" from the volatile _volN establishes a synchronization point between the 2 threads, which means all actions from t1 taken before the assignment to _volN <em>must</em> be visible to t2.</p> <p>Further reading:</p> <ul> <li><a href="http://www.ibm.com/developerworks/library/j-jtp02244.html" rel="noreferrer">Fixing the java memory model, part 1</a></li> <li><a href="http://www.ibm.com/developerworks/library/j-jtp03304/" rel="noreferrer">Fixing the java memory model, part 2</a></li> </ul>
 

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