Instruction reordering & happens-before relationship [duplicate]

The key point of the program order rule is: in a thread. Imagine this simple program (all variables initially 0): T1: x = 5; y = 6; T2: if (y == 6) System.out.println(x); From T1’s perspective, an execution must be consistent with y being assigned after x (program order). However from T2’s perspective this does … Read more

Why does this Java program terminate despite that apparently it shouldn’t (and didn’t)?

Obviously the write to currentPos doesn’t happen-before the read of it, but I don’t see how that can be the issue. currentPos = new Point(currentPos.x+1, currentPos.y+1); does a few things, including writing default values to x and y (0) and then writing their initial values in the constructor. Since your object is not safely published … Read more