in what architectures/OS other thread can see default nonfinal field values after constructor call? in what architectures/OS other thread can see default nonfinal field values after constructor call? multithreading multithreading

in what architectures/OS other thread can see default nonfinal field values after constructor call?


Alpha. Paul E. McKenney's book Is Parallel Programming Hard, And, If So, What Can You Do About It? has a chapter explaining thememory model of the most important platforms.


To get the result you want, you might try to turn on heavy optimization, so run the program in -server mode.

I first thought of making f volatile, but that would obviously screw the whole experiment.

Turn on the XML logging for the just-in-time compiler (if you are using the HotSpot JVM), and look at the generated machine code (using some external debugger or memory dumper). Then you can examine the generated code if that would even allow you to observe the result you want.


I suggest you acquire a copy of "Java Concurrency in Practice" and read Chapter 3 which details the JVM guarantees around locking and visibility. Your question has nothing to do with a specific architecture and everything to do with understanding happens-before in Java.

I think that you cannot repro the problem because there is a happens-before edge at the end of the FinalFieldExample constructor which is guaranteeing that x = 3 and y = 4;

BTW. The FinalFieldExample object is a bit of a mess. It wants to be a proper singleton, but you didn't code it that way. The lack of synchronization around the static "f" makes it more difficult than it should be to reason about the runtime behavior of this class. Methinks it should be a proper singleton with synchronization protecting access to the static "f" and you should be calling the writer and reader methods like...

FinalFieldExample.getInstance().writer();

Just sayin'