Quick revision
The short answer
Double-checked locking shows that a concurrency idiom can look obviously right, pass every test and run in production for years while still being wrong under the Java Memory Model. The lesson for an organisation is to base correctness on the specification and on well-reviewed library code, to prefer simple standard constructs, and to treat any clever low-level pattern as a rare exception that needs expert review, an owner and a place in an inventory that is revisited when the JDK or the hardware changes.
What to remember
In this answer, we saw that double-checked locking without volatile could publish a reference before the constructor's writes were visible, and that the Java 5 memory model fixed it by giving volatile happens-before semantics. The organisational lessons follow from that history: tests and production experience do not prove concurrent code correct, so correctness should rest on the specification and on well-reviewed library code; simple standard constructs are the default; custom low-level patterns are rare, reviewed and recorded in an inventory that is revisited on JDK and hardware changes; and engineers learn the mechanism through the story so they can spot the next variant.
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.
Double-checked locking shows that a concurrency idiom can look obviously right, pass every test and run in production for years while still being wrong under the Java Memory Model. The lesson for an organisation is to base correctness on the specification and on well-reviewed library code, to prefer simple standard constructs, and to treat any clever low-level pattern as a rare exception that needs expert review, an owner and a place in an inventory that is revisited when the JDK or the hardware changes.
Why this question is asked
Almost every large Java codebase has a lazy singleton or a cached field written in the double-checked style, and many of them were written by copying an example from somewhere. Interviewers ask this question at a senior level because the answer is not really about one idiom: it is about how a team decides what concurrent code it trusts, and how it protects itself from code that works only by luck. In this answer, we will recap what double-checked locking is, see exactly why it was broken before Java 5 and what fixed it, and then draw the organisational lessons: why tests are not proof, why the specification is the contract, how to review and inventory risky code, and how to teach the reasoning so that engineers recognise the next idiom of this kind.
1A Quick Recap of Double-Checked Locking
Double-checked locking is a way to create an object lazily, only when it is first needed, without paying for a lock on every call. The idea is simple: check whether the instance exists without a lock, and only if it is missing, take the lock, check again and create it. The second check is there because another thread may have created the instance while we were waiting for the lock.
The version below is the one that circulated widely in the late 1990s. It has no volatile on the field, and for years most people believed it was correct.
Notice that the fast path, the first check, reads the field without any synchronization. That unsynchronized read is where the whole problem lives.
2Why It Was Broken Before Java 5
The line instance = new Config() looks like one step, but it is really three: allocate memory, run the constructor to fill the fields, and store the reference into instance. Neither the compiler, the JIT nor the processor has to keep those steps in that order as seen by another thread. They may store the reference first and write the fields afterwards, because to the thread doing the work the result is the same.
So a second thread could pass the first check, see a non-null reference, skip the lock and read fields that still held their default values, such as a null map or a zero timeout. Under the original memory model, even adding volatile did not help, because old volatile semantics did not stop ordinary writes from moving past the volatile write.
JSR-133 rewrote the Java Memory Model for Java 5. It defined happens-before, a rule that says when the effects of one action are guaranteed to be visible to another, and it gave volatile real ordering power: everything a thread wrote before a volatile write is visible to any thread that later reads that volatile and sees the new value. With private static volatile Config instance, the idiom became correct.
6 messages in order. Play them, or step through them one at a time.
Without volatile nothing orders the constructor writes before the publication, so thread B may see default values.
3Lesson One: Passing Tests Is Not Proof
The broken idiom was used for years in serious products. The reason it survived is that the failure needs a narrow timing window, a particular JIT decision and, often, a processor with a weaker memory ordering than the developer's machine. On x86, which keeps stores in order much more strictly than the specification requires, the bad interleaving was very rare. Unit tests ran on one thread or on a laptop, and load tests seldom hit the exact moment of first initialisation.
So the code looked right to reviewers, passed tests and behaved in production, and it was still wrong. That is the key point a senior engineer takes from this history: for concurrent code, observed behaviour is weak evidence. A test can show that a bug exists; it can almost never show that one does not. The only reliable argument is a proof against the rules of the memory model, and very few people can write those proofs well.
4Lesson Two: Rest Correctness on the Specification and on Library Code
If tests cannot prove correctness, something else has to: the Java Language Specification, chapter 17 in particular, and the documented guarantees of java.util.concurrent, which the JDK tests on every supported platform. Code that relies only on those guarantees is correct on any conforming JVM and processor, not just on the ones we tested.
In practice this means we rarely need to write double-checked locking at all. The standard alternatives below are simpler and their correctness comes from the specification or from well-reviewed library code.
Notice that the holder idiom has no visible synchronization at all. It relies on a guarantee the JLS gives for class initialisation, which is far easier to review than a hand-written memory ordering argument.
5Lesson Three: Treat Clever Idioms as Reviewed Exceptions
An organisation does not need to ban low-level concurrency, but it should make it unusual and visible. A practical policy has three parts. The default is the standard construct: immutable objects, a holder class, a concurrent collection, an AtomicReference or a plain synchronized block. Anything below that level, such as hand-rolled publication, racy caching, VarHandle access modes or lock-free algorithms, needs a written reason, ideally a benchmark that shows the simple version is not good enough, and a review by someone who knows the memory model.
Static analysis helps here. Tools such as SpotBugs and Error Prone have checks that flag double-checked locking on a non-volatile field and similar patterns (check your tool version for the exact rule names). Running them in the build turns a rule that people must remember into one the pipeline enforces.
- Does a standard construct do the job?YesUse it, no special reviewNoContinue
- Is there a measured need for the custom version?YesContinueNoReject, use the simple construct
- Expert review against the JLS rulessomeone who can explain the happens-before edges
- Record it in the inventory with an ownerand a test or stress harness such as jcstress
4 steps in order. Play them, or step through them one at a time.
Custom low-level code is allowed, but it is the exception and it is always visible.
6Lesson Four: Keep an Inventory and Revisit It
Code that was correct only by luck can break when the luck changes. Three kinds of change matter most: a JDK upgrade that brings a new JIT optimisation, a move to different hardware such as ARM servers, whose memory ordering is weaker than x86, and a change in load that makes a rare interleaving common. Moving a fleet to ARM-based cloud instances is a realistic example; it is exactly the kind of change that exposes a missing volatile that x86 had been hiding.
So we keep a short inventory of the places where the codebase does something unusual with concurrency: who owns each one, why it exists and how it was verified. Before a JDK upgrade or a hardware migration, we walk that list. If it is short, that walk takes an afternoon. A grep for synchronized blocks around a null check, or a static analysis report, is a good way to build the first version of the list in a legacy codebase.
7Lesson Five: Teach the Reason Through the History
Rules without reasons fade. An engineer who only remembers 'add volatile' will not recognise the same mistake in another shape, such as a lazily built cache map published through a plain field, or a stop flag polled without volatile. An engineer who understands why double-checked locking broke, that a reference can become visible before the object is complete, recognises every variant. So the story belongs in onboarding and code review guides: it is short, it has a concrete mechanism, and it carries the general lesson that concurrent code must be correct under the rules, not merely seem to work.
8Follow-up questions
Is double-checked locking with a volatile field correct today?
Yes. Since Java 5, a volatile write publishes everything written before it, so a thread that reads a non-null value from the volatile field also sees the fully constructed object. It is correct, but for static singletons the holder class idiom is simpler and just as fast, so most teams prefer it.
Why did the bug rarely show up on x86 machines?
x86 processors keep stores in program order as seen by other cores, much more strictly than the Java Memory Model requires. So the hardware rarely produced the bad ordering, and only compiler reordering could expose it. On processors with weaker ordering, such as ARM, the window is wider, which is why hardware migrations are a good moment to revisit such code.
How would you find variants of this pattern in a large legacy codebase?
I would start with static analysis rules that flag double-checked locking and inconsistent synchronization, then search for lazily initialised fields that are checked for null outside a lock, and for fields read by other threads without volatile or a lock. Each finding is either replaced with a standard construct or added to the reviewed inventory with an owner.
9Conclusion
In this answer, we saw that double-checked locking without volatile could publish a reference before the constructor's writes were visible, and that the Java 5 memory model fixed it by giving volatile happens-before semantics. The organisational lessons follow from that history: tests and production experience do not prove concurrent code correct, so correctness should rest on the specification and on well-reviewed library code; simple standard constructs are the default; custom low-level patterns are rare, reviewed and recorded in an inventory that is revisited on JDK and hardware changes; and engineers learn the mechanism through the story so they can spot the next variant.
Open with
The double-checked locking story is a good case study because the broken version looked right to almost everyone, passed tests and ran in production for years. It was wrong because without volatile the reference to the new object could become visible before the constructor's writes, so another thread could see a half-built object.
Then, if they want more
The Java 5 memory model fixed it by giving volatile happens-before semantics. What I take from that as a leader is that observed behaviour is weak evidence for concurrent code. So I want our correctness to rest on the Java specification and on java.util.concurrent, not on 'it works on our servers'.
In practice the default is the simple construct: a holder class for a lazy singleton, computeIfAbsent for a cache, immutable objects and plain locks. Anything cleverer needs a written reason, ideally a benchmark, a review by someone who can explain the happens-before edges, and static analysis in the build to catch the known patterns.
I also keep a short inventory of that unusual code with owners, and we walk it before JDK upgrades or a move to different hardware such as ARM, because that is when hidden bugs appear. Finally, I teach the history itself, because engineers who understand why the idiom broke recognise the same mistake in new shapes.
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 "Double-checked locking was widely used and broken before Java 5, then fixed by new...": https://scrivio-interview-library.netlify.app/java/java-memory-model/0c8d0505e2/