Can Java output 1, 0 ? I tested it very intensively and I cannot get this conclusion. I get only 1, 1 or 0, 0 or 0, 1 .
public class Main { private int x; private volatile int g;
Why?
In my opinion, you can get 1, 0 . My reasoning. g is volatile, therefore it provides preservation of memory order. So it looks like this:
actor1: (1) store(x, 1) (2) store(g, 1) (3) memory_barrier // on x86
and I see the following situation: reorder store(g, 1) to store(x,1) (memory_barrier after (2)). Now run thread # 2. So g = 1, x = 0 . Now we are waiting for the exit. What is wrong in my reasoning?
java memory-order
Gilgamesz
source share