Reading \"Java Concurrency In Practice\", there\'s this part in section 3.5:
public Holder holder;
public void initialize() {
holder = new Holder(42);
}
The reason why this is possible is that Java has a weak memory model. It does not guarantee ordering of read and writes.
This particular problem can be reproduced with the following two code snippets representing two threads.
Thread 1:
someStaticVariable = new Holder(42);
Thread 2:
someStaticVariable.assertSanity(); // can throw
On the surface it seems impossible that this could ever occur. In order to understand why this can happen, you have to get past the Java syntax and get down to a much lower level. If you look at the code for thread 1, it can essentially be broken down into a series of memory writes and allocations:
Because Java has a weak memory model, it is perfectly possible for the code to actually execute in the following order from the perspective of thread 2:
Scary? Yes but it can happen.
What this means though is that thread 2 can now call into assertSanity
before n
has gotten the value 42. It is possible for the value n
to be read twice during assertSanity
, once before operation #3 completes and once after and hence see two different values and throw an exception.
EDIT
According to Jon Skeet, the AssertionError
migh still occur with Java 8 unless the field is final.
I was also very puzzled by that example. I found a website that explains the topic thoroughly and readers might find useful: https://www.securecoding.cert.org/confluence/display/java/TSM03-J.+Do+not+publish+partially+initialized+objects
Edit: The relevant text from the link says:
the JMM permits compilers to allocate memory for the new Helper object and to assign a reference to that memory to the helper field before initializing the new Helper object. In other words, the compiler can reorder the write to the helper instance field and the write that initializes the Helper object (that is, this.n = n) so that the former occurs first. This can expose a race window during which other threads can observe a partially initialized Helper object instance.
The Java memory model used to be such that the assignment to the Holder
reference might become visible before the assignment to the variable within the object.
However, the more recent memory model which took effect as of Java 5 makes this impossible, at least for final fields: all assignments within a constructor "happen before" any assignment of the reference to the new object to a variable. See the Java Language Specification section 17.4 for more details, but here's the most relevant snippet:
An object is considered to be completely initialized when its constructor finishes. A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields
So your example could still fail as n
is non-final, but it should be okay if you make n
final.
Of course the:
if (n != n)
could certainly fail for non-final variables, assuming the JIT compiler doesn't optimise it away - if the operations are:
then the value could change between the two fetches.
The basic issue is that without proper synchronization, how writes to memory may manifest in different threads. The classic example:
a = 1;
b = 2;
If you do that on one thread, a second thread may see b set to 2 before a is set to 1. Furthermore, it's possible for there to be an unbounded amount of time between a second thread seeing one of those variables get updated and the other variable being updated.
looking at this from a sane perspective, if you assume that the statement
if(n != n)
is atomic (which I think is reasonable, but i dont know for sure), then the assertion exception could never be thrown.
Well, in the book it states for the first code block that:
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
And for the second code block:
Because synchronization was not used to make the Holder visible to other threads, we say the Holder was not properly published. Two things can go wrong with improperly published objects. Other threads could see a stale value for the holder field, and thus see a null reference or other older value even though a value has been placed in holder. But far worse, other threads could see an up-todate value for the holder reference, but stale values for the state of the Holder.[16] To make things even less predictable, a thread may see a stale value the first time it reads a field and then a more up-to-date value the next time, which is why assertSanity can throw AssertionError.
I think JaredPar has pretty much made this explicit in his comment.
(Note: Not looking for votes here -- answers allow for more detailed info than comments.)