SyntaxPilot
Courses/Low-Level Design/Lesson 47
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Low-Level Design · Lesson 47 of 90

Concurrency 2: visibility, volatile and the Java memory model

Java Memory ModelvisibilityCPU cachesreorderinghappens-beforevolatileatomicity vs visibilitysafe publicationdouble-checked lockingthread-safe lazy singletonimmutability for thread safetythread confinementThreadLocal

These are the notes for Low-Level Design (LLD) episode 47. Last episode, two threads changing the same data lost updates. Today's bug is stranger: one thread writes a value and the other thread never sees it. We start with waiters copying the cafe's sign into their notebooks, watch a printer thread ignore closing time forever, and learn the rules Java uses for sharing memory: the Java Memory Model (JMM), CPU caches, reordering and happens-before. Then volatile, why it is not atomic, safe publication, double-checked locking, the thread-safe lazy singleton, and two ways to avoid sharing problems entirely: immutability and thread confinement with ThreadLocal. Every program was actually run with javac Main.java && java -cp . Main (Java 21). Outputs are real; loop counts and race totals vary from run to run.

1. The real-life idea: the board and the notebooks

In the cafe In the computer
The board on the wall: OPEN / CLOSED Main memory: the real value all threads share
Each waiter's notebook copy CPU cache / register: a fast private copy near each core (and the compiler may also keep a value in a register)
The rule book: when must you re-check the board? The Java Memory Model: rules for when a write becomes visible to other threads
A bell rung whenever the board changes volatile: every read and write goes to the real, shared value
  • Cache: a small, fast memory next to a processor core that keeps copies of recently used values.
  • Register: one of a core's own tiny storage slots, the fastest place a value can be.

2. The problem: a printer that never stops

public class Main {
    static boolean open = true;                                   // the cafe is open
    public static void main(String[] args) throws InterruptedException {
        Thread printer = new Thread(() -> {
            long checks = 0;
            while (open) checks++;                                // keep working while the cafe is open
            System.out.println("printer stopped after " + checks + " checks");
        });
        printer.setDaemon(true);                                  // don't keep the program alive
        printer.start();
        Thread.sleep(2000);
        open = false;                                             // closing time!
        System.out.println("main: open = false");
        printer.join(3000);                                       // wait up to 3 seconds
        System.out.println("3 seconds later, printer still running? " + printer.isAlive());
    }
}

Output (real; the same on every run):

main: open = false
3 seconds later, printer still running? true
  • A daemon thread is a background thread that doesn't keep the program alive (the JVM exits when only daemons remain), so this program can end.
  • join(3000) waits at most 3 seconds; isAlive() says if the thread is still running.
  • main wrote false, but the printer never saw it.

3. Why: visibility, the JIT and reordering

  • Nothing tells Java that another thread may change open, so Java may assume it doesn't.
  • The JIT compiler (the part of the JVM that turns frequently run code into fast machine code while the program runs) read open once and turned while (open) checks++; into "if open, loop forever". That's legal for single-threaded code.
  • Compilers and CPUs also reorder independent steps (run them in a different order than written) when a single thread can't tell the difference. Other threads can.
  • Visibility: whether, and when, one thread's write can be seen by another thread.

Happens-before

The JMM's central rule: if action X happens-before action Y, then Y is guaranteed to see X's effects (and everything before X). Without a happens-before relationship, there is no guarantee at all.

Happens-before edge Example
Program order Within one thread, earlier statements happen-before later ones
Volatile write → later read of that variable ready = true (volatile) then another thread reads ready == true
Unlock → later lock of the same lock Leaving synchronized (lock) then another thread entering it
t.start() → everything in t Values set before start() are seen by the new thread
Everything in t → t.join() returning After join(), the joining thread sees all of t's writes
Transitivity If A hb B and B hb C, then A hb C
Thread A                         Thread B
price = 250;          (plain)
ready = true;         (volatile) ---- happens-before ---->  if (ready)          (volatile read sees true)
                                                               print(price)     guaranteed 250

4. The fix: volatile

public class Main {
    static volatile boolean open = true;                          // volatile: always read fresh
    public static void main(String[] args) throws InterruptedException {
        Thread printer = new Thread(() -> {
            long checks = 0;
            while (open) checks++;                                // keep working while the cafe is open
            System.out.println("printer stopped after " + checks + " checks");
        });
        printer.setDaemon(true);                                  // don't keep the program alive
        printer.start();
        Thread.sleep(2000);
        open = false;                                             // closing time!
        System.out.println("main: open = false");
        printer.join(3000);                                       // wait up to 3 seconds
        System.out.println("3 seconds later, printer still running? " + printer.isAlive());
    }
}

Output (real; the number of checks varies):

main: open = false
printer stopped after 2798516545 checks
3 seconds later, printer still running? false

A volatile field: every read sees the latest write by any thread, and the compiler/CPU may not cache it or reorder ordinary reads and writes across it in ways that break happens-before. Use it for flags (stop, ready) and for publishing a reference to a finished (ideally immutable) object.

5. volatile is not atomic

class OrderCounter {
    private volatile int count = 0;                               // volatile: visible... but atomic?
    void add() {                                                  // read, yield, write: like count++
        int c = count;
        Thread.yield();
        count = c + 1;
    }
    int get() { return count; }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        OrderCounter counter = new OrderCounter();
        Runnable cashier = () -> { for (int i = 0; i < 100_000; i++) counter.add(); };
        Thread a = new Thread(cashier), b = new Thread(cashier);
        a.start(); b.start(); a.join(); b.join();
        System.out.println("orders = " + counter.get() + " (expected 200000)");
    }
}

Output (real; always fewer than 200000: count++ written as read, Thread.yield(), write, exactly as in episode 46):

orders = 100216 (expected 200000)
Atomicity Visibility
Means No other thread can get in between the steps Other threads see the result
volatile No Yes
synchronized Yes (for code using the same lock) Yes (unlock happens-before the next lock)
AtomicInteger (episode 48) Yes, for single variables Yes

Because visibility through synchronized comes from unlock → lock, reads need the lock too, not just writes.

6. Safe publication: the lazy singleton race

Lazy initialization: creating an object only when it is first needed. A singleton (episode 22) is a class with exactly one instance.

class Menu {                                                      // expensive to load: load it ONCE
    private static Menu instance;
    private Menu() {
        System.out.println("loading menu... by " + Thread.currentThread().getName()); pause(100);
    }
    static Menu getInstance() {
        if (instance == null) instance = new Menu();              // check-then-act!
        return instance;
    }
    static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Menu[] got = new Menu[2];                                 // what each cashier received
        Thread a = new Thread(() -> got[0] = Menu.getInstance(), "cashier-1");
        Thread b = new Thread(() -> got[1] = Menu.getInstance(), "cashier-2");
        a.start(); b.start(); a.join(); b.join();
        System.out.println("both cashiers got the same menu? " + (got[0] == got[1]));
    }
}

Output (real; the order of the two "loading" lines varies):

loading menu... by cashier-2
loading menu... by cashier-1
both cashiers got the same menu? false

Two "singletons": if (instance == null) instance = new Menu(); is check-then-act (episode 46). Making getInstance() synchronized fixes it but takes the lock on every call, forever.

Double-checked locking, with volatile

class Menu {
    private static volatile Menu instance;                        // volatile: safe publication
    private Menu() {
        System.out.println("loading menu... by " + Thread.currentThread().getName()); pause(100);
    }
    static Menu getInstance() {
        Menu m = instance;
        if (m == null) {                                          // 1st check: no lock (fast path)
            synchronized (Menu.class) {
                m = instance;
                if (m == null) instance = m = new Menu();         // 2nd check: under the lock
            }
        }
        return m;
    }
    static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Menu[] got = new Menu[2];                                 // what each cashier received
        Thread a = new Thread(() -> got[0] = Menu.getInstance(), "cashier-1");
        Thread b = new Thread(() -> got[1] = Menu.getInstance(), "cashier-2");
        a.start(); b.start(); a.join(); b.join();
        System.out.println("both cashiers got the same menu? " + (got[0] == got[1]));
    }
}

Output (real):

loading menu... by cashier-1
both cashiers got the same menu? true
  • 1st check without the lock: the fast path once the menu exists.
  • 2nd check under the lock: another thread may have created it while we waited.
  • Reading the field once into a local m avoids a second volatile read.

Why the field must be volatile

instance = new Menu(); is really: (1) get memory, (2) run the constructor, (3) store the reference. Without volatile, (2) and (3) may be reordered: another thread's unlocked first check can see a non-null instance pointing to a half-built object. A volatile write publishes the object only after it is fully built: everything before the write happens-before any read that sees the reference.

Safe publication = making an object visible to other threads only in a way that guarantees they see it fully constructed: through a volatile field, a final field, a lock, a static initializer, or a thread-safe collection.

Double-checked locking without volatile is a famous broken pattern (it may fail very rarely, which makes it worse).

7. The simplest thread-safe lazy singleton: the holder idiom

class Menu {
    private Menu() { System.out.println("loading menu... by " + Thread.currentThread().getName()); }
    private static class Holder {                                 // loaded only on first use
        static final Menu INSTANCE = new Menu();                  // the JVM runs this once, thread-safely
    }
    static Menu getInstance() { return Holder.INSTANCE; }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("program started, menu not loaded yet");
        Menu[] got = new Menu[2];                                 // what each cashier received
        Thread a = new Thread(() -> got[0] = Menu.getInstance(), "cashier-1");
        Thread b = new Thread(() -> got[1] = Menu.getInstance(), "cashier-2");
        a.start(); b.start(); a.join(); b.join();
        System.out.println("both cashiers got the same menu? " + (got[0] == got[1]));
    }
}

Output (real):

program started, menu not loaded yet
loading menu... by cashier-1
both cashiers got the same menu? true

The JVM initializes a class once, thread-safely, on first use. Holder is first used inside getInstance(), so the menu is lazy, and our code has no volatile and no synchronized. The other simple option is an enum singleton (practice 1).

8. Avoid sharing: immutability and thread confinement

import java.util.*;
record Prices(Map<String, Integer> menu) {                        // immutable: safe to share
    Prices { menu = Map.copyOf(menu); }                           // final field + unmodifiable copy
}
public class Main {
    static final ThreadLocal<String> cashier = new ThreadLocal<>();   // one value PER THREAD
    static void log(String msg) { System.out.println("[" + cashier.get() + "] " + msg); }   // no parameter
    static void bill(Prices p, String item) {
        StringBuilder receipt = new StringBuilder();              // local variable: confined to this thread
        receipt.append(item).append(" Rs ").append(p.menu().get(item));
        log(receipt.toString());
    }
    public static void main(String[] args) throws InterruptedException {
        Prices prices = new Prices(new HashMap<>(Map.of("Thali", 250, "Chai", 20)));
        Thread a = new Thread(() -> { cashier.set("cashier-1"); bill(prices, "Thali"); });
        Thread b = new Thread(() -> { cashier.set("cashier-2"); bill(prices, "Chai"); });
        a.start(); b.start(); a.join(); b.join();
        log("main has no cashier set");
        try { prices.menu().put("Thali", 1); }                    // can anyone change the prices?
        catch (UnsupportedOperationException e) { System.out.println("can't change prices: " + e); }
    }
}

Output (real; the order of the two cashier lines varies):

[cashier-2] Chai Rs 20
[cashier-1] Thali Rs 250
[null] main has no cashier set
can't change prices: java.lang.UnsupportedOperationException
  • Immutability for thread safety: an object whose state can't change after construction (all fields final, no setters, defensive copies like Map.copyOf). The JMM guarantees that final fields are seen correctly by every thread once the constructor finishes, so immutable objects can be shared freely without locks.
  • Thread confinement: data only one thread ever touches needs no lock. Local variables (like the StringBuilder receipt) are confined automatically: each thread has its own stack.
  • ThreadLocal: gives each thread its own separate value of one variable (set, get, remove). Here, the current cashier, available deep in log() without passing a parameter. A thread that never set it gets null.
  • With thread pools (episode 48) threads are reused, so a forgotten ThreadLocal value leaks into the next task: always remove() in a finally (practice 2).

Common mistakes

Mistake What you see Why Fix
Plain boolean stop flag No error: printer still running? true No happens-before; the JIT hoisted the read volatile boolean
volatile int count; count++ No error: fewer than 200000 Visible, not atomic synchronized or AtomicInteger
Reading shared data without the lock Stale values Visibility needs unlock → lock Lock reads too (or volatile)
Lazy init without a lock No error: both cashiers got the same menu? false Check-then-act Holder idiom, enum, or synchronized
Double-checked locking without volatile Very rarely, a half-built object Reordering volatile field, or the holder idiom
Publishing this from a constructor (e.g. starting a thread in it) Other thread sees half-built object Object not finished Start threads after construction
Forgetting ThreadLocal.remove() in pools Last task's value leaks Threads are reused try { ... } finally { tl.remove(); }

Mini project: live settings with safe publication

A worker reads a volatile reference to an immutable Config; changing settings = publishing a new object with one volatile write. A volatile flag stops the worker.

import java.util.*;
record Config(int discountPercent, List<String> specials) {        // immutable snapshot of settings
    Config { specials = List.copyOf(specials); }
}
class Cafe {
    private volatile Config config = new Config(0, List.of("Chai"));   // publish whole objects via volatile
    private volatile boolean running = true;
    void update(Config c) { config = c; }                          // one volatile write = safe publication
    void stop() { running = false; }
    void serve() {
        Config seen = null;
        while (running) {
            Config c = config;                                      // read once, use a consistent snapshot
            if (c != seen) { System.out.println("worker sees: " + c); seen = c; }
        }
        System.out.println("worker stopped");
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Cafe cafe = new Cafe();
        Thread worker = new Thread(cafe::serve);
        worker.start();
        Thread.sleep(300); cafe.update(new Config(10, List.of("Chai", "Samosa")));
        Thread.sleep(300); cafe.stop();
        worker.join(2000);
        System.out.println("worker alive: " + worker.isAlive());
    }
}

Output (real):

worker sees: Config[discountPercent=0, specials=[Chai]]
worker sees: Config[discountPercent=10, specials=[Chai, Samosa]]
worker stopped
worker alive: false

Practice

1. Write the simplest thread-safe singleton with an enum.

Answer
enum Menu {                                                       // the simplest thread-safe singleton
    INSTANCE;
    Menu() { System.out.println("loading menu once, by " + Thread.currentThread().getName()); }
    int price(String item) { return item.equals("Thali") ? 250 : 20; }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(() -> Menu.INSTANCE.price("Thali"), "cashier-1");
        Thread b = new Thread(() -> Menu.INSTANCE.price("Chai"), "cashier-2");
        a.start(); b.start(); a.join(); b.join();
        System.out.println("thali costs " + Menu.INSTANCE.price("Thali"));
    }
}

Output (real; which cashier loads it varies):

loading menu once, by cashier-2
thali costs 250

The JVM creates enum constants once, thread-safely, when the enum class is initialized. Enums are also safe against reflection and serialization tricks (episode 22).

2. Show a ThreadLocal value leaking between tasks in a thread pool, and fix it.

Answer
import java.util.concurrent.*;
public class Main {
    static final ThreadLocal<String> cashier = new ThreadLocal<>();
    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(1);   // ONE reused thread (episode 48)
        pool.submit(() -> cashier.set("cashier-1")).get();        // task 1 sets it and forgets to remove
        pool.submit(() -> System.out.println("task 2 sees: " + cashier.get())).get();   // leaked!
        pool.submit(() -> { try { cashier.set("cashier-3"); } finally { cashier.remove(); } }).get();
        pool.submit(() -> System.out.println("task 4 sees: " + cashier.get())).get();   // clean
        pool.shutdown();
    }
}

Output (real):

task 2 sees: cashier-1
task 4 sees: null

Task 1 never removed its value, so the reused thread still carried cashier-1 into task 2. Task 3 used try/finally with remove(), so task 4 starts clean.

3. Why does making count volatile not fix count++?

Answer

volatile only makes each read and write visible; count++ is still three steps (read, add, write), and two threads can interleave between them and lose an update.

4. A thread sets data = 42; and then calls t.start() on a new thread t, which prints data (a plain field). Is 42 guaranteed?

Answer

Yes. Everything before t.start() happens-before everything in t.

Quick quiz

int price = 0; volatile boolean ready = false;

// Thread A
price = 250;
ready = true;

// Thread B
if (ready) System.out.println(price);

What is guaranteed? (a) Prints 0 or 250 (b) If it prints, it prints 250 (c) Never prints (d) Compile error

Answer

(b). The volatile write of ready happens-before B's read that sees true, so B also sees price = 250. If B looks before A writes, it prints nothing.

Interview questions

  1. What is the Java Memory Model? The part of the Java specification that defines when writes by one thread are guaranteed visible to reads in another, and which reorderings are allowed.
  2. What is happens-before? A guarantee that one action's effects are visible to another; created by program order, volatile write/read, unlock/lock, start, join, and transitivity.
  3. What does volatile guarantee? Visibility of the latest write, and ordering (no reordering across it that breaks happens-before). Not atomicity of compound actions.
  4. volatile vs synchronized? volatile: visibility only, no blocking. synchronized: mutual exclusion plus visibility.
  5. Why can a thread loop forever on a non-volatile flag? The JIT may hoist the read out of the loop (and caches may hold old values); without happens-before, Java never has to re-read it.
  6. Why is double-checked locking broken without volatile? Construction and assignment can be reordered, publishing a reference to a partially constructed object.
  7. Best ways to write a lazy thread-safe singleton? Holder idiom or enum; DCL with volatile if you must.
  8. What is safe publication? Making an object visible to other threads only via a mechanism that guarantees they see it fully constructed (volatile, final fields, locks, static init, concurrent collections).
  9. Why are immutable objects thread-safe? Their state never changes after construction, and final field semantics guarantee every thread sees the constructed values.
  10. What is ThreadLocal for, and its pitfall? Per-thread values (current user, request id, non-thread-safe helpers). In pools, values leak between tasks unless removed.

Glossary

Term Meaning, with an example
Java Memory Model (JMM) Java's rules for when one thread's writes are visible to another
Main memory The shared memory all threads read and write, e.g. the board on the wall
CPU cache A small fast memory beside a core keeping copies of values, e.g. a waiter's notebook
Register A core's own tiny, fastest storage slot
JIT compiler The part of the JVM that compiles hot code to fast machine code while the program runs
Visibility Whether and when a write by one thread can be seen by another
Reordering Compiler/CPU running independent steps in a different order than written
Happens-before A guarantee that one action's effects are visible to another action
volatile A field modifier: reads always see the latest write; no harmful reordering around it
Atomicity Steps happen as one; nobody can get in between
Atomicity vs visibility Not interleaved vs seen by others; volatile gives only visibility
Daemon thread A background thread that doesn't keep the JVM alive: t.setDaemon(true)
join(ms) / isAlive() Wait at most ms for a thread / is it still running?
Lazy initialization Create an object only when first needed
Safe publication Making an object visible only when it is fully constructed
Double-checked locking Check without lock, lock, check again; needs a volatile field
Thread-safe lazy singleton One lazily created instance, safe under many threads: holder idiom or enum
Holder idiom A private static nested class holding the instance; the JVM initializes it once, on first use
Immutability State can't change after construction; safe to share without locks
final field semantics Final fields are seen correctly by all threads after the constructor finishes
Thread confinement Data used by only one thread, e.g. local variables
ThreadLocal One separate value per thread: set, get, remove
Thread pool A set of reused threads that run many tasks (episode 48)

Summary

  • Without happens-before, a thread may never see another thread's write (JIT hoisting, caches, reordering).
  • Happens-before comes from program order, volatile write → read, unlock → lock, start, join.
  • volatile = visibility, not atomicity: count++ still races.
  • Publish objects safely: DCL needs volatile; prefer the holder idiom or an enum singleton.
  • Best of all, don't share mutable state: immutable objects, confined data, ThreadLocal (remove it in pools).

Next episode: Concurrency 3: ReentrantLock, tryLock, read-write locks, atomic variables and compare-and-swap, and thread pools with ExecutorService.