There a multiple code examples which assume that the following instructions (1) and (2) cannot be reordered:
int value;
volatile boolean ready;
// ...
value = 1; // (1)
ready = true; // (2)
The latter Stack Overflow answer refers to JLS §17.4.5:
If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).
However I don't understand why this should apply here since the JLS Example 17.4-1 also states:
[...] compilers are allowed to reorder the instructions in either thread, when this does not affect the execution of that thread in isolation.
which is clearly the case here.
All other definitions in the JLS specific to volatile
are only with respect to the same volatile variable, but not to other actions:
A write to a volatile field (§8.3.1.4) happens-before every subsequent read of that field.
It confuses me where people see the guarantee that usage of volatile (read or write) may not be reordered.
Could you please base you explanation on the JLS or on other sources which are based on the JLS.