'How can a store to a variable be reordered after a load of that variable, given single-threaded serialization guarantee?

Lets have a simple code snippet

   import java.invoke.VarHandle.fullFence;
   ...
   int x = 42;
   int y = 42;
   ...
   x = 1;
   fullFence();
   x = 0;
   y = x;
   fullFence();

   //another thread
   if (y == 1)
      System.err.println("wtf?");

There is another thread which reads x and y - I am trying to reason what kind of guarantees it gets from this code, before adding fences itself.

Considering that the thread executing that snippet must see y==0 after the second fence, can load(x) and store(x, 0) from between the fences be actually reordered with each other? If so, then why?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source