Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the example the that book is giving the Holder class is not directly the cause of the problem, in fact it states that: </p> <p><em>The problem here is not the Holder class itself, but that the Holder is not properly published. However, Holder can be made immune to improper publication by declaring the n field to be final, which would make Holder immutable; see Section 3.5.2.</em></p> <p>Just prior to this it mentions the following code, which it the subject of the problem:</p> <pre><code>// Unsafe publication public Holder holder; public void initialize() { holder = new Holder(42); } </code></pre> <p>So to re-create it you will need to create a publisher class and two threads, one that calls initialize and one that calls the assert.</p> <p>Having said that, I tried to re-create it myself and still failed to do so :( </p> <p>Below is my first attempt, however there is a better explanation of the problem at <a href="http://forums.oracle.com/forums/thread.jspa?threadID=1140814&amp;tstart=195" rel="nofollow">http://forums.oracle.com/forums/thread.jspa?threadID=1140814&amp;tstart=195</a></p> <pre><code>public class HolderTest { @Test public void testHolder() throws Exception { for (int i = 0; i &lt; 1000000000; i++) { final CountDownLatch finished = new CountDownLatch(2); final HolderPublisher publisher = new HolderPublisher(); final Thread publisherThread = new Thread(new Publisher(publisher, finished)); final Thread checkerThread = new Thread(new Checker(publisher, finished)); publisher.holder = null; publisherThread.start(); checkerThread.start(); finished.await(); } } static class Publisher implements Runnable { private final CountDownLatch finished; private final HolderPublisher publisher; public Publisher(final HolderPublisher publisher, final CountDownLatch finished) { this.publisher = publisher; this.finished = finished; } @Override public void run() { try { publisher.initialize(); } finally { finished.countDown(); } } } static class Checker implements Runnable { private final CountDownLatch finished; private final HolderPublisher publisher; public Checker(final HolderPublisher publisher, final CountDownLatch finished) { this.publisher = publisher; this.finished = finished; } @Override public void run() { try { publisher.holder.assertSanity(); } catch (final NullPointerException e) { // This isnt the error we are interested in so swallow it } finally { finished.countDown(); } } } static class HolderPublisher { // Unsafe publication public Holder holder; public void initialize() { holder = new Holder(42); } } } </code></pre>
    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.
    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