I'm now reading Java Language Specification.
§17.4.5-1
said
In this execution, the reads see writes that occur later in the execution order. This may seem counterintuitive, but is allowed by happens-before consistency. Allowing reads to see later writes can sometimes produce unacceptable behaviors.
§17.4.8-1
said
Although allowing reads to see writes that come later in the execution order is sometimes undesirable, it is also sometimes necessary.
Also 17.4.8-1 gives a strange example.
Why can reads see writes come later
possible?
If it's really possible, how can I reproduce it in java code?
EDIT
This is not duplicate question. That question just asked 17.4.5-1, I can understand 17.4.5-1 because compiler may reorder them. But what about 17.4.8-1? It's under Executions and Causality Requirements
. According to definition of execution order
, no one can reorder
r1 = x; // write
and
if (r1 != 0) // read
So that y = 1
must happens the last.