Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If another thread were to check the <code>someObject</code> variable "during" construction, I believe it <em>may</em> (due to quirks in the memory model) see a partially initialized object. The new (as of Java 5) memory model means that any <em>final</em> fields should be set to their values before the object becomes visible to other threads (so long as the reference to the newly created object doesn't escape from the constructor in any other way) but beyond that there aren't many guarantees.</p> <p>Basically, don't share data without appropriate locking (or guarantees given by static inializers etc) :) Seriously, memory models are seriously tricky, as is lock-free programming in general. Try to avoid this becoming a possibility.</p> <p>In <strong>logical</strong> terms the assignment happens <em>after</em> the constructor runs - so if you observe the variable <em>from the same thread</em> it will be null during the constructor call. However, as I say there are memory model oddities.</p> <p>EDIT: For the purposes of double-checked locking, you can get away with this <em>if</em> your field is <code>volatile</code> and <em>if</em> you're using Java 5 or higher. Prior to Java 5 the memory model wasn't strong enough for this. You need to get the pattern <em>exactly</em> right though. See Effective Java, 2nd edition, item 71 for more details.</p> <p>EDIT: Here's my reasoning for arguing against Aaron's inlining being visible in a single thread. Suppose we have:</p> <pre><code>public class FooHolder { public static Foo f = null; public static void main(String[] args) { f = new Foo(); System.out.println(f.fWasNull); } } // Make this nested if you like, I don't believe it affects the reasoning public class Foo { public boolean fWasNull; public Foo() { fWasNull = FooHolder.f == null; } } </code></pre> <p>I believe this will <em>always</em> report <code>true</code>. From <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26" rel="noreferrer">section 15.26.1</a>:</p> <blockquote> <p>Otherwise, three steps are required:</p> <p><ul> <li>First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.</li> <li>Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs. </ul></li> Otherwise, the value of the right-hand operand is converted to the type of the left-hand variable, is subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.</p> </blockquote> <p>Then from <a href="http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4.5" rel="noreferrer">section 17.4.5</a>:</p> <p>Two actions can be ordered by a happens-before relationship. If one action happens-before another, then the first is visible to and ordered before the second.</p> <blockquote> <p>If we have two actions x and y, we write hb(x, y) to indicate that x happens-before y.</p> <ul> <li>If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).</li> <li>There is a happens-before edge from the end of a constructor of an object to the start of a finalizer (§12.6) for that object.</li> <li>If an action x synchronizes-with a following action y, then we also have hb(x, y).</li> <li>If hb(x, y) and hb(y, z), then hb(x, z). </li> </ul> <p>It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal.</p> </blockquote> <p>In other words, it's okay for weird stuff to happen even within a single thread <em>but that mustn't be observable</em>. In this case the difference <em>would</em> be observable, which is why I believe it would be illegal.</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