Consider the following Java code:
volatile boolean v1 = false;
volatile boolean v2 = false;
v1 = true;
if (v2)
System.out.println("v2 was true");
v2 = true;
if (v1)
System.out.println("v1 was true");
If there was a globally visible common order for unstable accesses, then at least one println would always be achieved.
Is it really guaranteed by the Java standard? Or is this execution as possible:
A: v1 = true;
B: v2 = true;
A: read v2 = false;
B: read v1 = false;
A: v2 = true becomes visible (after the if)
B: v1 = true becomes visible (after the if)
I could only find statements about access to the same mutable variable in the standard (but I could miss something).
"Writing to a mutable variable (ยง8.3.1.4) v is synchronized with all subsequent readings v by any thread (where the next is determined in accordance with the synchronization order).
http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4.4
Thank!
source
share