Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>What would happen if instead of declaring b as volatile I put the write and read into a synchronized block?</p> </blockquote> <p><em>If and only if</em> you protect all such synchronized blocks with the same lock will you have the same guarantee of visibility as with your <code>volatile</code> example. You will in addition have mutual exclusion of the execution of such synchronized blocks.</p> <blockquote> <blockquote> <p>Don't use volatile to protect more than one piece of shared state.</p> </blockquote> <p>Why?</p> </blockquote> <p><code>volatile</code> does not guarantee atomicity: in your example the <code>s</code> variable may also have been mutated by other threads after the write you are showing; the reading thread won't have any guarantee as to which value it sees. Same thing goes for writes to <code>s</code> occurring after your read of the <code>volatile</code>, but before the read of <code>s</code>.</p> <p>What is safe to do, and done in practice, is <em>sharing immutable state</em> transitively accessible from the reference written to a <code>volatile</code> variable. So maybe that's the meaning intended by "one piece of shared state".</p> <blockquote> <p>is it possible to puzzle that statement together from the official Java documentation/Oracle sources?</p> </blockquote> <p>Quotes from the spec:</p> <blockquote> <h3>17.4.4. Synchronization Order</h3> <p>A write to a volatile variable v (§8.3.1.4) synchronizes-with all subsequent reads of v by any thread (where "subsequent" is defined according to the synchronization order).</p> <h3>17.4.5. Happens-before Order</h3> <p>If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).</p> <p>If an action x synchronizes-with a following action y, then we also have hb(x, y).</p> </blockquote> <p>This should be enough.</p> <blockquote> <p>And from which version of Java onwards will this work?</p> </blockquote> <p>Java Language Specification, 3rd Edition introduced the rewrite of the Memory Model specification which is the key to the above guarantees. NB most previous versions acted as if the guarantees were there and many lines of code actually depended on it. People were surprised when they found out that the guarantees had in fact not been there.</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. 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