Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Is the following safe when the proxiedObject is being acceessed by multiple threads?</p> </blockquote> <p>No, unless <code>mo</code> is <code>volatile</code>. If the <code>mo</code> field is <code>volatile</code> then this will properly cross a memory barrier and the updates to the <code>mo</code> field will be seen by all threads.</p> <p>It is important to realize that if the <code>MyObject</code> is not immutable, additional concurrency issues will result even if <code>mo</code> is <code>volatile</code>.</p> <hr> <p><strong>Edit:</strong></p> <p>Thanks to @jtahlborn comments on this. I've been doing some reading and I'm now pretty sure that the <code>volatile</code> will <em>also</em> protect the <code>MyObject</code> from being partially initialized.</p> <p>Due to <a href="http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html" rel="nofollow noreferrer">constructor instruction reordering possibilities</a>, there is no guarantee that the <code>MyObject</code> has been fulling constructed when its referenced is shared between threads. Only <code>final</code> fields in a constructor are guaranteed to be properly initialized when a constructor finishes. Any other fields may or may not have been initialized and so you will need to synchronize on the object before the multiple threads can start accessing it.</p> <p>However, if the<code>mo</code> field is volatile, then the "happens before" guarantee also ensures that the <code>MyObject</code> has been fully initialized. The compiler is not allowed to reorder those instructions past the publishing of a <code>volatile</code> variable. See:</p> <ul> <li><a href="http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html" rel="nofollow noreferrer">What Volatile Means In Java</a></li> <li><a href="https://stackoverflow.com/questions/2441279/java-volatile-guarantees-and-out-of-order-execution">Volatile guarantees and out-of-order execution</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