Java double checked locking

Double check locking is broken. Since initialized is a primitive, it may not require it to be volatile to work, however nothing prevents initialized being seen as true to the non-syncronized code before instance is initialized. EDIT: To clarify the above answer, the original question asked about using a boolean to control the double check … Read more

Why is volatile used in double checked locking

As quoted by @irreputable, volatile is not expensive. Even if it is expensive, consistency should be given priority over performance. There is one more clean elegant way for Lazy Singletons. public final class Singleton { private Singleton() {} public static Singleton getInstance() { return LazyHolder.INSTANCE; } private static class LazyHolder { private static final Singleton … Read more