Why did the double-checked locking singleton need volatile to be correct?
Reviewed by Gurusankar M.
β‘ Short Answer
Object construction isn't atomic: the JIT can publish the reference BEFORE the constructor finishes. Without volatile, a second thread can see a non-null but partially-constructed instance. Marking the field volatile forbids that reordering and ensures safe publication.
βCoffee Chat Question
Concept Made Simple
βWhy did the double-checked locking singleton need volatile to be correct?β
π§ Mind Map Answer
Remember It Faster
instance = new Singleton() is really: allocate β construct β assign. Reordering can make the assign visible before construct completes β another thread sees a half-built object. volatile blocks the reorder.
β¨οΈHands-on Keyboard
Learn by Doing
private static volatile Singleton inst; // volatile is essential
static Singleton get() {
if (inst == null) {
synchronized (Singleton.class) {
if (inst == null) inst = new Singleton();
}
}
return inst;
}π₯What If?
Think Beyond the Expected
Is there a simpler correct lazy singleton that avoids this subtlety?
Yes β the initialization-on-demand holder idiom: a private static holder class whose static field is the instance. The JVM's class-init guarantees lazy, thread-safe, single creation with no locking or volatile. An enum singleton is also safe.
πReal World
DCL is a favorite hard interview question because it exposes deep memory-model understanding; in practice teams use the holder idiom or enum, or a DI container, rather than hand-written DCL.
π―Interviewer's Expectation
Keywords they're listening for:
β οΈCommon Mistakes
- βOmitting volatile on the instance field
- βHand-rolling DCL instead of using the holder idiom
- βAssuming object construction is atomic
β Best Practices
- βPrefer the holder idiom or enum singleton
- βIf using DCL, the field MUST be volatile
- βLet a DI framework manage singletons
πFollow-up Questions
- 1How does the holder idiom guarantee thread safety?
- 2Why is an enum the most robust singleton?
- 3What changed in the Java 5 memory model to make DCL fixable?
π§©Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Locks (Multithreading) Interview question: "Why did the double-checked locking singleton need volatile to be correct?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.