...
In this noncompliant code example, the Helper
class is made immutable by declaring its fields final. The Java Memory Model (JMM) guarantees that immutable objects are fully constructed before they become visible to any other thread. The block synchronization in the getHelper()
method guarantees that all threads that can see a non-null value of the helper field will also see the fully-initialized Helper
object.
...
However, this code is not guaranteed to be succeed on all JVM platforms because there is no happens-before relationships relationship between the first read and third read of helper
. Consequently, it is possible for the third read of helper
to obtain a stale null value (perhaps because its value was cached or reordered by the compiler) causing the getHelper()
method to return a null pointer.
...