Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>1.</p> <p>Based on what you say here</p> <blockquote> <p>constructing a new object, setting its fields and setting the next field to the object pointed by the root, then setting the root's next field to this new node. </p> </blockquote> <p>Then yes, setting the next field to volatile will correctly synchronize. Its important to understand why. You have three sets of writes before hand, the one to the node object, one to the fields and one to the nodes next (though not completely sure why you are doing that, maybe I miss understand something). </p> <p>So that's 2 + (N number of field) writes. At this point there is no happens-before relationship and if the node is written normally there is no guarantee. As soon as you write to the volatile field all previous writes will now also be visible.</p> <p>2.</p> <p>Volatile reads/writes on a x86 (or any cache-coherent) operating system has the following attributes: </p> <blockquote> <pre><code> volatile-read: very close to a normal read volatile-write: about 1/3 the time of a synchronization write (whether within intrinsic locking or j.u.c.Lock locking) </code></pre> </blockquote> <p>3.</p> <p>Looks like you will have to create VolatileNode and Node. There was a proposal for Java 7 to come out with a <a href="http://gee.cs.oswego.edu/dl/jsr166/dist/docs/java/util/concurrent/atomic/Fences.html" rel="noreferrer">Fences</a> API which you can specify which style of reading/write you want to execute with a static utility class but doesn't look like its releasing </p> <p>Edit:</p> <p>Thkala made a great point I feel is worth including</p> <blockquote> <p>although it should be pointed out that pre-JSR133 JVMs (i.e. Java &lt; 5.0) did not have the same semantics</p> </blockquote> <p>So what I wrote does not apply to applications run in Java 1.4 or less.</p>
 

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