Java Reference
In-Depth Information
Table 17.3. Surprising results caused by forward substitution
One common compiler optimization involves having the value read for
r2
reused for
r5
: they are both reads of
r1.x
with no intervening write. This situation is shown in
Table 17.4. Surprising results caused by forward substitution
Now consider the case where the assignment to
r6.x
in Thread 2 happens between the
first read of
r1.x
and the read of
r3.x
in Thread 1. If the compiler decides to reuse the
value of
r2
for the
r5
, then
r2
and
r5
will have the value
0
, and
r4
will have the value
3
.
From the perspective of the programmer, the value stored at
p.x
has changed from
0
to
3
and then changed back.
The memory model determines what values can be read at every point in the program. The
actions of each thread in isolation must behave as governed by the semantics of that thread,
with the exception that the values seen by each read are determined by the memory model.
When we refer to this, we say that the program obeys
intra-thread semantics
. Intra-thread
semantics are the semantics for single-threaded programs, and allow the complete predic-
tion of the behavior of a thread based on the values seen by read actions within the thread.
To determine if the actions of thread
t
in an execution are legal, we simply evaluate the im-