The only problem in this code that I see is that the βcachedConstructorβ field is mutable, while it guarantees the visibility effect among the threads, this particular code block has the fancy that different threads can see that cachedConstructor is equal null before the value will be assigned by one of the threads, i.e. the initialization sequence is not atomic. This can only lead to the fact that cachedConstructor can be assigned a couple of times at the same time, but will not violate the code if no one believes that it will be the same instance of Constructor. If the cachedConstructor initialization block is synchronized, it will be atomic, i.e. CachedConstructor is assigned only once, regardless of race condition.
However, the code should work as expected, but just allows you to simultaneously redistribute the cached value to more than one thread.
Ievgen Lukash
source share