Code Room
Code reviewHardcr-g218
Subject Race conditionsLevel Senior–Staff~40 minCommon in Concurrency interviewsIndustries Software development

Question

Review this Java lazy-singleton initialization.

Is this double-checked locking correct? If not, what is the precise failure?

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewjava
public final class ConfigCache {    private static ConfigCache instance;          // not volatile    private final Map<String, String> settings;     private ConfigCache() {        settings = loadFromDisk();                // populates the map    }     public static ConfigCache get() {        if (instance == null) {                   // first check (no lock)            synchronized (ConfigCache.class) {                if (instance == null) {           // second check                    instance = new ConfigCache(); // (1)                }            }        }        return instance;    }}
Run or narrate your approach, then ask the coach.