Code RoomResource visibility without synchronization
HardPrep Room Coding #1869

Resource visibility without synchronization

Code reviewConcurrencySenior–Staff~40 min

Review this Java holder that publishes an object through a non-volatile field.

Assume `initOnce()` runs exactly once, before readers are told to start. Is unsynchronized publication safe here?

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
1class Holder {
2 private Resource res; // not volatile, not final
3 
4 void initOnce() { // called by one init thread
5 Resource r = new Resource();
6 r.setup(); // sets internal fields
7 this.res = r; // (1) publish
8 }
9 Resource getResource() { // called by many reader threads
10 return res; // (2) read without sync
11 }
12}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.