Concurrent access to a public field. Why is it possible to observe inconsistent state?
<p>I'm reading <a href="http://ift.tt/1Yt5dB6" rel="nofollow">B. Goetz Java Concurrency In practice</a> and now I'm at the <code>section 3.5</code> about safe publication. He stated:</p><pre><br /><code>// Unsafe publication<br />public Holder holder;<br />public void initialize() {<br /> holder = new Holder(42);<br />}<br /></code><br /></pre><blockquote><p>This improper publication could allow another thread to observe a partially constructed object.</p></blockquote><p>I don't see why it is possible to observe a partially constructed subobject. Assume, that the constructor <code>Holder(int)</code> does not allow <code>this</code> to escape. So, the constructed reference can be observed only by the caller. Now, as <a href="http://ift.tt/1RQTJDj" rel="nofollow">JLS 17.7</a> stated:</p><blockquote><p>Writes to and reads of references are always atomic, regardless of whether they are implemented as 32-bit or 64-bit values.</p></blockquote><p>it is impossible for thread to observe a partially constructed object.</p><p>Where was I wrong?</p><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34524969/concurrent-access-to-a-public-field-why-is-it-possible-to-observe-inconsistent">http://ift.tt/1MGl6b6</a></h2>
Nhận xét
Đăng nhận xét