1

Below code are from Java Concurrency in Practice

//Listing 3.14 Unsafe publication
public Holder holder;
public void initialize() {
    holder = new Holder(42);
}

//Listing 3.15
public class Holder {
    private int n;
    public Holder(int n) { this.n = n; }
    public void assertSanity() {
        if (n != n)
            throw new AssertionError("This statement is false.");
    }
}

The author said:

In fact, if the Holder in Listing 3.15 is published using the unsafe publication idiom in Listing 3.14, and a thread other than the publishing thread were to call assertSanity, it could throw AssertionError!

Could anybody provide some code snippet that can lead to the AssertionError as the author said?

Thanks

mikea
  • 6,537
  • 19
  • 36
July
  • 855
  • 4
  • 11
  • 22

0 Answers0