Java Specification: reads see writes that occur later in the execution order

185 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

The example

Table 17.4.8-A

      Thread 1                  Thread 2
r1 = x;                    r2 = y;
if (r1 != 0) y = 1;        if (r2 != 0) x = 1;

is possible on an architecture that has speculative execution. So the CPU encounters the conditional branch when the value requested from the main memory has not arrived yet and decides to execute the conditional code and roll it back in case the condition is not fulfilled.

When the value arrives at the CPU core, it’s the value written by the other thread, so the condition is fulfilled and the changes are kept.

You can not reproduce this in Java code, as the specification continues with the explanation that such a scenario is not allowed for Java code. It’s an example of what would be allowed, if we only had happens-before consistency. But Java additionally forbids “out of thin air values”. So, JVM implementations for architectures supporting such speculative execution must ensure that it is limited to variables not seen by other threads (or disable it completely).