Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

Explain what can go wrong with this code under concurrency:

Java
class Config {
private static Config instance;
static Config get() {
if (instance == null) {
synchronized (Config.class) {
if (instance == null) {
instance = new Config();
}
}
}
return instance;
}
}

The first null check reads instance without synchronisation, and because instance is not volatile, nothing orders the write of the reference against the writes inside the Config constructor. Another thread can therefore see a non-null instance whose fields still hold default values and use a half-built Config. Declaring the field volatile fixes this under the Java Memory Model as revised in Java 5, and the initialization-on-demand holder idiom avoids the problem with no explicit synchronisation at all.

Why this question is asked

This code looks careful: it checks twice, it locks, and on most laptops it never fails. That is what makes it dangerous, and why interviewers show it. The bug is invisible in single-threaded reading and rare in testing, and it can only be explained with the Java Memory Model, the set of rules that says when one thread's writes become visible to another. In this answer, we will see what the code tries to do, find the unsynchronised read, follow how a half-built object becomes visible, explain why volatile fixes it, and finish with the holder idiom, which avoids the question entirely.

1What the Code Is Trying to Do

The goal is a lazily created singleton: one Config, created on the first call to get(), shared by all threads. Synchronising the whole method would work, but every call would take a lock, so the code tries to be clever. It checks for null without the lock, and only if the instance seems missing does it lock and check again.

The second check inside the synchronized block is correct and necessary. If two threads both see null, the first creates the instance while the second waits at the lock; when the second enters, it sees the instance and does not create another. So the code does prevent two instances. The problem is somewhere else.

2The Unsynchronised First Read

A synchronized block gives two guarantees: only one thread runs it at a time, and when a thread enters it, it sees everything written by the previous thread before that thread left it. But the second guarantee only applies to threads that also synchronise on the same lock.

The first check, if (instance == null), runs outside the lock. A thread that finds instance already non-null never enters the synchronized block, so it has no guarantee about what it sees. It reads whatever value the field has in its view of memory, and it then reads Config's fields with no ordering promise at all.

Java
static Config get() {
if (instance == null) { // read with no synchronisation and no volatile
synchronized (Config.class) {
if (instance == null) {
instance = new Config(); // write happens inside the lock
}
}
}
return instance; // fast path: returns without ever locking
}

3How a Half-Built Config Becomes Visible

The line instance = new Config() is not one step. It allocates memory for the object, runs the constructor, which writes the fields, and stores the reference in instance. The Java Memory Model allows the compiler and the CPU to reorder these writes as long as the thread doing them cannot tell the difference. So from another thread's point of view, the store of the reference may become visible before the stores made by the constructor.

Java
// What thread A writes, in program order:
Config tmp = allocate(); // fields hold defaults: url = null, timeout = 0
tmp.url = "jdbc:postgresql://db/app";
tmp.timeout = 30;
instance = tmp;
// What thread B may observe without volatile:
instance != null // true
instance.url // null
instance.timeout // 0
Step throughThread B sees the reference before the fields
Thread AMemoryThread B
allocate Config, fields at defaults
instance = reference (reordered earlier)
first check: instance == null?
not null
read instance.timeout
0, constructor writes not yet visible
timeout = 30, url = ... become visible

7 messages in order. Play them, or step through them one at a time.

Without volatile nothing forces A's constructor writes to be visible before the reference.

Notice that thread B did nothing wrong by its own logic: it saw a non-null reference and returned it. But the object it got is not the object the constructor built. It may connect to a null URL, or use a timeout of zero. The failure depends on the JIT, the CPU's memory model and timing, which is why it may never appear on a developer's machine and then appear under load on a server with many cores.

4Why volatile Fixes It

Since Java 5, when JSR 133 revised the Java Memory Model, volatile has a strong meaning. A write to a volatile field happens-before every later read of that field by any thread. Happens-before is the model's ordering promise: everything thread A did before the volatile write, including all the constructor's field writes, is visible to thread B once B reads the value that write stored.

Java
class Config {
private static volatile Config instance; // the only change
static Config get() {
Config c = instance; // volatile read
if (c == null) {
synchronized (Config.class) {
c = instance;
if (c == null) {
instance = c = new Config(); // volatile write after construction
}
}
}
return c; // always fully constructed
}
}

So with volatile, the compiler and CPU may not move the constructor's writes after the write of the reference, and thread B either sees null and takes the lock, or sees the reference and also sees the completed fields. Before Java 5, volatile did not give this guarantee, which is why older articles call double-checked locking broken in all forms. On modern Java, the volatile version is correct. The local variable c is an optimisation so the fast path reads the volatile field only once.

5The Holder Idiom: No Explicit Synchronisation

We can avoid the whole question by letting the JVM do the lazy initialisation. Class initialisation is guaranteed to happen once, under a lock the JVM manages, and its results are visible to every thread that uses the class afterwards.

Java
class Config {
private Config() { /* read settings */ }
private static final class Holder {
static final Config INSTANCE = new Config();
}
static Config get() {
return Holder.INSTANCE; // Holder initialised on first call, once, safely
}
}

Holder is initialised only when get() first touches it, so the creation is still lazy. After that, every call is a plain read of a static final field. There is no volatile, no synchronized and nothing to get wrong, which is why most reviewers prefer this version in new code.

6Follow-up questions

Would making the fields of Config final fix it without volatile?

Final fields do have a special guarantee: if the object does not leak during construction, any thread that sees a reference to it sees the final fields correctly initialised. So a Config whose state is entirely in final fields is safe from the half-built problem. But it is fragile, because one non-final field added later reintroduces the bug, so volatile or the holder idiom is the clearer fix.

How could we test for this bug?

Ordinary unit tests almost never catch it. The OpenJDK jcstress tool runs small concurrent tests millions of times on real hardware and reports outcomes that the memory model allows, such as seeing a non-null reference with default fields. In practice, the better defence is code review and using the holder idiom or volatile from the start.

Is the bug possible on x86 processors?

x86 has a relatively strong memory model, so the CPU rarely shows this reordering, but the JIT compiler is allowed to reorder the writes itself. The code is incorrect according to the Java Memory Model, and it can fail on ARM servers, which have a weaker memory model and are now common in the cloud.

7Conclusion

In this answer, we saw that the code prevents two instances but not a half-built one. The first null check reads instance without synchronisation, and without volatile the write of the reference can become visible before the constructor's field writes, so another thread can return a Config with default values. A volatile field adds the happens-before edge that orders them, which is correct since the Java 5 memory model, and the holder idiom gives the same safety with no explicit synchronisation.

What to say in the interviewAbout 90 seconds aloud

Open with

The code correctly stops two threads from both creating a Config, because of the second check inside the lock, but it can still hand out a half-built object.

Then, if they want more

The first null check reads instance outside any synchronisation. Creating the object involves allocating it, running the constructor's field writes and storing the reference, and without volatile, the compiler or CPU may make the reference visible to other threads before the field writes.

So a second thread can see that instance is not null, skip the lock, return it, and read null or zero from fields the constructor was supposed to set. It is very timing dependent, so it rarely shows up in tests, and it is more likely on ARM servers. The fix is to declare instance volatile.

Since Java 5 and the revised memory model, a volatile write happens-before any later read of it, so everything the constructor wrote is visible to a thread that sees the reference. I would also read the field into a local variable so the fast path does one volatile read.

The alternative I usually prefer is the holder idiom: a private static nested class holding a static final instance, because the JVM's class initialisation is lazy, runs once and is thread-safe without any locking in my code.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "Explain what can go wrong with this code under concurrency:": https://scrivio-interview-library.netlify.app/java/creational-patterns/83f6633436/