No, there are no synchronization rules for instance types, for example, to initialize classes. You must add them yourself. Regardless of whether you are doing this with double checked locking or some other mechanism, you can.
Starting with Java 8, I want to use ConcurrentHashMap#computeIfAbsent to achieve laziness.
class Bob { private final ConcurrentHashMap<String, Jane> instance = new ConcurrentHashMap<>(1); public Jane getJane() { return instance.computeIfAbsent("KEY", k -> new Jane());
There are also these solutions for lazy initialization without limiting thread safety. I could not easily adapt them for a multi-threaded context.
As pointed out in the comments, a double checked lock will always be faster than these solutions, since they do not include all the fluff to hide the implementation.
Sotirios delimanolis
source share