Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the short summary:</p> <ul> <li>For references, reads/writes are <em>ALWAYS</em> atomic (even in 64 bit implementations!)</li> <li>For <code>int</code>, <code>char</code>, <code>byte</code>, <code>short</code>, <code>boolean</code>, <code>float</code>, reads/writes are <em>ALWAYS</em> atomic</li> <li>For <code>double</code> and <code>long</code>, if they're <code>volatile</code>, reads/writes are <em>ALWAYS</em> atomic</li> </ul> <p>Therefore there is only exception where reads/writes may not be atomic:</p> <ul> <li>For <code>double</code> and <code>long</code>, if they're <em>NOT</em> declared <code>volatile</code>, they're <em>NOT GUARANTEED</em> to be atomic</li> </ul> <p>So as far as atomicity of reading/writing shared data is concerned, you only need to make <code>volatile</code> any <code>double</code> or <code>long</code>. Everything else is already guaranteed to be atomic, regardless of how many bits are used in actual implementation.</p> <hr> <h3>On the specification</h3> <p>Here's the relevant section reproduced here for quick reference:</p> <blockquote> <h3><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.7" rel="noreferrer">JLS 17.7 Non-atomic Treatment of <code>double</code> and <code>long</code></a></h3> <p>Some implementations may find it convenient to divide a single write action on a 64-bit <code>long</code> or <code>double</code> value into two write actions on adjacent 32 bit values. For efficiency's sake, this behavior is implementation specific; Java virtual machines are free to perform writes to <code>long</code> and <code>double</code> values atomically or in two parts.</p> <p>For the purposes of the Java programming language memory model, a single write to a non-<code>volatile long</code> or <code>double</code> value is treated as two separate writes: one to each 32-bit half. This can result in a situation where a thread sees the first 32 bits of a 64 bit value from one write, and the second 32 bits from another write. Writes and reads of <code>volatile long</code> and <code>double</code> values are always atomic. Writes to and reads of references are always atomic, regardless of whether they are implemented as 32 or 64 bit values.</p> <p>VM implementors are encouraged to avoid splitting their 64-bit values where possible. Programmers are encouraged to declare shared 64-bit values as <code>volatile</code> or synchronize their programs correctly to avoid possible complications.</p> </blockquote> <h3>See also</h3> <ul> <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.6" rel="noreferrer">JLS 17.6 Word Tearing</a> - guarantees e.g. a <code>byte</code> can be updated without neighbor interference</li> <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.4" rel="noreferrer">JLS 8.3.1.4 <code>volatile</code> Fields</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html" rel="noreferrer">Java Tutorials/Essential Classes/Concurrency/Atomic Variables</a> <ul> <li>Note that given <code>int i;</code>, <code>i++</code> is <em>NOT</em> atomic!</li> </ul></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/q/3038203/587642">Is there any point in using a volatile long?</a></li> <li><a href="https://stackoverflow.com/q/2236184/587642">How to declare array elements volatile in Java?</a> <ul> <li>A <code>volatile long[]</code> is <code>volatile</code> <em>reference</em> to an array of <code>long</code></li> <li>The <code>long</code> elements themselves are not <code>volatile</code></li> </ul></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