Quick revision
The short answer
The first read of instance happens outside the lock, so a thread can see a non-null reference to a Config whose fields are not yet visible, because the write of the reference can be reordered with the writes inside the constructor. Fix 1 is to declare instance volatile, which makes double-checked locking correct since Java 5. Fix 2 is the holder-class idiom, a private static class Holder { static final Config INSTANCE = new Config(); }, which relies on the JVM's class initialisation; an enum with a single constant is another option.
What to remember
In this answer, we saw that this double-checked locking is broken because the first check reads instance without the lock, and the reference can become visible before the constructor's writes, so a thread can return a half-built Config. Declaring instance volatile fixes it since Java 5, because the volatile write publishes everything the constructor wrote. The holder-class idiom gets the same laziness from the JVM's class initialisation with no locking code, and a single-constant enum is a third correct option.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The first read of instance happens outside the lock, so a thread can see a non-null reference to a Config whose fields are not yet visible, because the write of the reference can be reordered with the writes inside the constructor. Fix 1 is to declare instance volatile, which makes double-checked locking correct since Java 5. Fix 2 is the holder-class idiom, a private static class Holder { static final Config INSTANCE = new Config(); }, which relies on the JVM's class initialisation; an enum with a single constant is another option.
Why this question is asked
Lazy singletons are everywhere in older Java code, and this exact pattern, double-checked locking, was once recommended in books and articles. It looks careful: it checks twice and takes a lock. It is still broken, and the reason is one of the clearest illustrations of why we need the Java Memory Model. Interviewers ask it to see whether we can explain the failure precisely and give correct alternatives. In this answer, we will see what the code is trying to do, trace how a thread can get a half-built Config, apply the volatile fix and see why it works, and then look at the holder idiom and the enum.
1What the Code Is Trying to Do
The goal is a lazily created singleton: create Config only when someone first asks for it, and never create two. The simplest correct version makes get() synchronized, but then every call takes the lock, even years after the instance was created. Double-checked locking tries to avoid that cost.
The idea is that check 1 skips the lock once the instance exists, and check 2 prevents two threads that both passed check 1 from creating two instances. The second part is fine. The problem is check 1.
2How a Thread Can See a Half-Built Config
The statement instance = new Config() is really several steps: allocate memory, run the constructor, which writes values and timeoutMs, and store the reference into instance. Within the creating thread, the order does not matter as long as the result is the same, so the compiler and the CPU are allowed to make the store of the reference visible before the writes inside the constructor.
Now look at a second thread. Its check 1 reads instance without the lock. There is no happens-before edge between the creating thread's writes and this read, because the second thread never takes the lock. So it can see a non-null instance, skip the synchronized block, and return a Config whose timeoutMs still reads as 0 from its point of view.
5 messages in order. Play them, or step through them one at a time.
Thread B never takes the lock, so nothing orders its reads after Thread A's constructor.
Notice that the lock does not help the second thread at all, because it never enters the lock. The lock only orders things for threads that acquire it. The values field is final, so the final field guarantee does cover it, but timeoutMs is not final and has no protection at all, and relying on final fields to rescue a racy read is fragile once someone adds a non-final field.
3Fix 1: Declare instance volatile
Adding volatile to the field makes the pattern correct, as long as we run on Java 5 or later, where JSR-133 strengthened volatile.
The reason it works is the volatile rule: a write to a volatile field happens-before every later read of it that sees the value. The constructor's writes come before the volatile write in program order, so a thread whose volatile read sees the non-null reference also sees everything the constructor wrote. The JVM is no longer allowed to make the reference visible before those writes. The local variable c is a small refinement: it reads the volatile field once on the fast path instead of twice.
4Fix 2: The Holder-Class Idiom
The holder idiom uses the JVM's own lazy, thread-safe class initialisation instead of writing any locking ourselves.
A class is initialised the first time it is actively used, not when the program starts, and the JVM guarantees that initialisation runs exactly once, under a lock, with its results visible to every thread that uses the class afterwards. Holder is used for the first time inside get(), so Config is created lazily on the first call. Later calls read a static final field of an already-initialised class, with no synchronization cost. This is usually the best choice for a lazy static singleton: short, obviously correct, and fast.
5Another Option: A Single-Constant Enum
An enum with one constant is also a singleton, created during the enum's class initialisation, so it is thread-safe in the same way.
| Option | Lazy? | Cost after creation | Notes |
|---|---|---|---|
| volatile double-checked locking | Yes | One volatile read | Works for instance fields too |
| Holder-class idiom | Yes, on first get() | Plain static read | Static singletons only |
| Single-constant enum | Yes, on first use | Plain static read | Serialization-safe |
| Original code, no volatile | Yes | - | Broken: half-built object visible |
The enum also handles two corner cases for free: serialization cannot create a second instance, and reflection cannot call the constructor. It is lazy in the same sense as the holder, at first use of the enum class. Its downsides are that it cannot extend another class, and it looks unusual to some readers, so many teams prefer the holder idiom for ordinary singletons.
6Follow-up questions
Would making all fields of Config final fix the original code?
Mostly, because the final field guarantee lets any thread that sees the reference see the final fields as constructed, even through a race. But it only covers final fields and what they reach, so any non-final field or later change is unprotected, and the code still relies on a racy read of instance. volatile or the holder idiom is the clear, reviewable fix.
Why was double-checked locking broken before Java 5 even with volatile?
Under the original memory model, volatile only ordered volatile accesses relative to each other; it did not publish the ordinary writes made before it. So the constructor's writes could still be seen after the volatile reference. JSR-133 in Java 5 made a volatile write publish all earlier writes to a thread that reads the value, which is what makes Fix 1 correct.
Do we still need lazy singletons in a Spring application?
Rarely. Spring creates singleton beans and publishes them safely during startup, and @Lazy beans are also created under the container's own synchronization. Static lazy singletons mostly remain in libraries that run outside a container, where the holder idiom is the usual choice.
7Conclusion
In this answer, we saw that this double-checked locking is broken because the first check reads instance without the lock, and the reference can become visible before the constructor's writes, so a thread can return a half-built Config. Declaring instance volatile fixes it since Java 5, because the volatile write publishes everything the constructor wrote. The holder-class idiom gets the same laziness from the JVM's class initialisation with no locking code, and a single-constant enum is a third correct option.
Open with
The problem is the first check. It reads instance outside the lock, so there is no happens-before edge between the thread that creates the Config and a thread that reads the field on the fast path. instance = new Config() is really allocate, run the constructor and store the reference, and the compiler or CPU may make the store of the reference visible before the writes inside the constructor.
Then, if they want more
So another thread can see instance as non-null, skip the lock entirely, and return a Config whose fields still look like defaults, for example a timeout of zero. The lock does not help that thread, because it never takes the lock. The first fix is to declare instance volatile.
Since Java 5, a volatile write happens-before every read that sees it, and the constructor's writes come before that write, so any thread that sees the reference sees a fully built object. I would also read the field into a local so the fast path does one volatile read.
The second fix is the holder-class idiom: a private static nested class Holder with a static final INSTANCE = new Config(), and get returns Holder.INSTANCE. The JVM initialises Holder on first use, exactly once, under a lock, and makes the result visible to all threads, so it is lazy, thread-safe and has no synchronization cost afterwards.
That is my default for static singletons. A third option is an enum with a single constant, which is also lazily initialised by the JVM and is safe against serialization and reflection creating a second instance.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is wrong with this lazy singleton, and give two correct alternatives.": https://scrivio-interview-library.netlify.app/java/java-memory-model/459570840b/