Code RoomNon-volatile field in double-checked lock
HardPrep Room Coding #1853

Non-volatile field in double-checked lock

Code reviewConcurrencySenior–Staff~40 min

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.

0:00 of about 40 min
Mark a line and say what kind of problem it is.0 findings
1public final class ConfigCache {
2 private static ConfigCache instance; // not volatile
3 private final Map<String, String> settings;
4 
5 private ConfigCache() {
6 settings = loadFromDisk(); // populates the map
7 }
8 
9 public static ConfigCache get() {
10 if (instance == null) { // first check (no lock)
11 synchronized (ConfigCache.class) {
12 if (instance == null) { // second check
13 instance = new ConfigCache(); // (1)
14 }
15 }
16 }
17 return instance;
18 }
19}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.