Skip to content
Java Concurrency Deep Dive: JMM, Locks, AQS, Virtual Threads & Structured Concurrency

Java Concurrency Deep Dive: JMM, Locks, AQS, Virtual Threads & Structured Concurrency

This comprehensive guide covers Java concurrency fundamentals from JMM three properties and volatile semantics to synchronized lock upgrades, AQS internals, CAS/ABA solutions, ThreadLocal memory leaks, JDK 21 virtual threads, and JDK 26 structured concurrency with code examples and interview-focused insights.

Rain's Path to Java Mastery
Rain's Path to Java Mastery
Rain's Path to Java Mastery

Concurrency Three Pillars: Atomicity, Visibility, Ordering

These three properties form the foundation of Java concurrency. All thread-safety issues (dirty reads, deadlocks, heisenbugs) stem from failures to guarantee them correctly.

Atomicity

Definition : An operation is an indivisible unit — either fully executes or not at all, cannot be interrupted.

Root cause : A single Java statement compiles to multiple CPU instructions; time-slicing can switch threads mid-operation.

Classic counterexample : count++ is NOT atomic. It splits into three steps: read from main memory to thread working memory, increment in working memory, write back to main memory.

Java guarantees : synchronized (pessimistic lock), java.util.concurrent.atomic (CAS optimistic lock), Lock interface.

Classic Pitfall: i++ Is Not Atomic

int count = 0; // 10 threads each execute count++ 1000 times

JVM executes three steps: read current value, add 1, write back. Thread switching between any two steps causes lost updates; final result < 10000.

Visibility

Definition : When one thread modifies a shared variable, other threads immediately see the latest value.

Root cause : JMM mandates variables in main memory; each thread has private working memory. Operations occur in working memory, not directly on main memory.

Classic counterexample : Thread sets stopFlag=true, another thread loops forever on stale false.

Java guarantees : volatile (force flush on write, force read from main memory), synchronized (flush on unlock), Lock (flush on unlock), final (visible after initialization).

Shared flag variables must be volatile.

// Thread A
boolean stop = false;
while(!stop) { /* do work */ }

// Thread B sets stop = true // B modifies working-memory copy, not flushed; A never sees it!

Ordering

Definition : Program executes strictly in code order.

Root cause : Compiler and CPU reorder instructions provided single-threaded result unchanged. Multi-threaded observers see chaotic order.

Classic counterexample : Double-checked locking (DCL) singleton without volatile yields half-initialized object → NPE.

Java guarantees : volatile (forbids reordering), synchronized (single-threaded execution in block), JMM Happens-Before rules (core ordering criterion).

Reordering Disaster: DCL Singleton

public class Singleton {
    private static Singleton instance;
    public static Singleton getInstance() {
        if (instance == null) { // first check
            synchronized(Singleton.class) {
                if (instance == null) { // second check
                    instance = new Singleton(); // problem here!
                }
            }
        }
        return instance;
    }
}
instance = new Singleton()

compiles to three steps: allocate memory, initialize object, assign reference. JIT may reorder to 1→3→2. Thread A reaches step 3 before initialization completes; Thread B sees non-null instance and returns a half-initialized object.

Three Properties Quick Reference

Atomicity — Core issue: operation interrupted mid-way; Root cause: CPU time-slice scheduling; Main guarantees: synchronized, Atomic classes, Lock; Typical scenarios: counters, transfers, inventory deduction.

Visibility — Core issue: modification not perceived timely; Root cause: JMM working-memory mechanism; Main guarantees: volatile, synchronized, Lock; Typical scenarios: state flags, switch controls.

Ordering — Core issue: instruction order scrambled; Root cause: compiler/CPU reordering; Main guarantees: volatile, synchronized, Happens-Before; Typical scenarios: singleton, safe publication.

How They Work Together

A volatile variable guarantees visibility and ordering, but count++ compound operations still lack atomicity. Conversely, synchronized blocks cover all three but incur higher overhead. Choose per scenario: flag → volatile; counter increment → AtomicLong; compound condition checks → synchronized / Lock.

volatile Keyword: Visibility + Reordering Prevention, Atomicity?

Two Core Roles

Role 1: Cross-Thread Visibility

Mechanism: volatile write flushes immediately to main memory; read fetches directly from main memory, invalidating other threads' cached copies.

Solves: One thread modifies variable, others still use stale cached value.

Role 2: Instruction Reordering Prevention

Mechanism: Compiler/CPU reorder for optimization; volatile inserts memory barriers before/after read/write to forbid specific reorderings.

Classic use: Double-checked locking (DCL) singleton to prevent half-initialized objects.

Can It Guarantee Atomicity? No

Explicit answer: No. This is volatile 's most common pitfall.

Classic Counterexample: i++ Atomicity Failure

public class VolatileAtomicDemo {
    private static volatile int count = 0;
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    count++;
                }
            }).start();
        }
        Thread.sleep(1000);
        System.out.println("Final result: " + count);
    }
}

Bytecode-Level Breakdown

count++

compiles to 4 bytecode instructions, a compound operation:

0: getstatic  #2  // read count from main memory (volatile guarantees latest)
3: iconst_1          // load constant 1
4: iadd              // execute addition
5: putstatic  #2    // write result back to main memory

Between iadd and putstatic, the thread can be descheduled; other threads modify count, causing stale write-back.

Three Synchronization Mechanisms Comparison

volatile — Visibility: full; Atomicity: no; Ordering: partial; Overhead: minimal (lock-free); Use cases: state flags, DCL.

synchronized — Visibility: full; Atomicity: full; Ordering: full; Overhead: higher (heavyweight lock); Use cases: sync blocks/methods.

AtomicInteger — Visibility: full; Atomicity: full; Ordering: full; Overhead: low (CAS lock-free); Use cases: atomic counters, updates.

Summary & Correct Usage

volatile

is a lightweight sync mechanism solving only visibility and ordering, never atomicity.

Correct: boolean flags ( boolean running = true), DCL singleton, one-time safe publication.

Incorrect: counters (i++, i--), any compound operation ( a = a + b).

synchronized Internals: Biased → Lightweight → Heavyweight Lock Upgrade

Lock upgrade centers on the object header Mark Word (8 bytes on 64-bit JVM). Mark Word content changes completely per lock state:

Unlocked : hashCode, generational age; lock flag 01, biased bit 0.

Biased : 54-bit thread ID + 2-bit epoch; biased bit 1, lock flag 01.

Lightweight : 62-bit ptr to stack Lock Record; lock flag 00.

Heavyweight : 62-bit ptr to OS mutex; lock flag 10.

GC Mark : lock flag 11.

Lock flag bits are the sole state discriminator; in 01 state, biased-bit distinguishes unlocked vs biased.

Phase-by-Phase Core Mechanics

Biased Lock (JDK 1.6 introduced, JDK 15 default off)

Philosophy: If same thread always acquires lock, record its ID in Mark Word; subsequent entries skip even CAS.

Trigger: Single thread repeatedly accesses same sync block, zero contention.

Mechanism: First thread CAS-writes its thread ID into Mark Word, sets biased-bit=1.

Advantage: Zero overhead! Subsequent entries only check Mark Word thread ID matches self — no CAS.

Revocation triggers: (1) Another thread competes; (2) hashCode() called (biased state has no room for hashCode).

Revocation requires global safepoint (STW) — why biased lock hurts performance under high contention.

Lightweight Lock

Philosophy: Assume contention ends quickly; spin instead of blocking, avoid OS switch cost.

Trigger: Biased lock revoked, or two threads alternating access (low contention).

Mechanism: Thread creates Lock Record in its stack frame; CAS replaces Mark Word with pointer to its Lock Record; losers spin-wait (burn CPU, no kernel transition).

Advantage: Avoids OS-level blocking/context-switch; outperforms heavyweight under low contention.

Adaptive spinning (JDK 1.6): Spin count dynamically adjusted based on prior spin time on same lock.

Heavyweight Lock

Philosophy: Contention fierce; spinning wastes CPU — must let OS park threads.

Triggers: (1) Spin count exceeds threshold (default 10, adaptive adjusts); (2) >2 threads simultaneously compete.

Mechanism: Mark Word replaced with pointer to OS Mutex; all losers blocked (user→kernel transition); unlock wakes all waiters to re-compete.

Drawback: Block/wake requires OS intervention, context-switch overhead huge — hence "heavyweight".

Upgrade direction is one-way irreversible (like a ratchet).

Three Locks Core Comparison

Biased — Scenario: single thread repeated access; Sync overhead: near zero; Thread state: running; Context switch: none; Performance: highest.

Lightweight — Scenario: multi-thread alternating; Sync overhead: few CAS ops; Thread state: running (spinning); Context switch: none; Performance: medium.

Heavyweight — Scenario: multi-thread fierce contention; Sync overhead: OS thread blocking; Thread state: blocked; Context switch: frequent; Performance: lowest.

Interview-Favorite Addenda

Upgrade is one-way: biased→lightweight→heavyweight, no downgrade.

Lock coarsening: JIT merges consecutive lock/unlock into larger sync block.

Lock elision: JIT removes sync on thread-local objects.

JDK 15 change: Biased lock disabled by default — modern apps multi-threaded, revocation cost exceeds benefit.

Biased lock vs hashCode mutual exclusion: hashCode() forces Mark Word to store hash; biased state has no space → immediate revocation. Objects without overridden hashCode() benefit most from biased lock.

Bulk rebias/revoke: JVM uses epoch; many objects of same class acquired by same thread → bulk rebias; frequent revocations by different threads → JVM disables biased for that class.

Lightweight reentrancy: Stacks Lock Records without modifying Mark Word — clever zero-overhead design.

"synchronized lock upgrade essentially adapts increasingly complex sync strategies to contention levels; core idea: trade space for time, spin for blocking, prediction for overhead."

synchronized vs ReentrantLock: Differences & Selection

Core Differences

Implementation: synchronized — JVM-level, based on Object Monitor; ReentrantLock — JDK-level, based on AQS.

Lock type: synchronized — reentrant, non-fair; ReentrantLock — reentrant, supports fair/non-fair (default non-fair).

Interrupt response: synchronized — no, blocks indefinitely; ReentrantLock — yes, lockInterruptibly().

Timeout acquisition: synchronized — no, waits forever; ReentrantLock — yes, tryLock(time, unit) returns false on timeout.

Multiple conditions: synchronized — single wait/notify queue; ReentrantLock — multiple Condition objects, precise wakeup.

Release: synchronized — auto on block exit/exception; ReentrantLock — manual, must call unlock() in finally.

Performance: synchronized — JDK 1.6+ optimized (biased/lightweight), better under low contention; ReentrantLock — more stable under high contention, higher throughput.

Complexity: synchronized — minimal, concise, hard to misuse; ReentrantLock — higher, manual acquire/release management.

Debugging: synchronized — easy, JVM built-in, jstack shows lock state; ReentrantLock — harder, requires AQS source analysis.

Fundamental Difference: Underlying Muscle

synchronized Upgrade Path

Unlocked → biased → lightweight → heavyweight.

ReentrantLock via AQS

All queuing, wakeup, reentrancy counts managed by AQS state variable + CLH queue — pure Java, no direct kernel entry.

Key Feature Deep-Dive

Reentrancy Essence

synchronized: Monitor records owner thread ID and recursion count; same thread re-acquires → count++; release → count--; zero releases lock.

ReentrantLock: AQS state tracks recursion; lock() CAS increments state, unlock() decrements; state=0 releases lock.

Fair vs Non-Fair

synchronized: Only non-fair; JVM lets current thread try first, then queue — better throughput but possible starvation.

ReentrantLock: Constructor true creates fair lock; strict FIFO — fair but ~10-20% throughput loss.

Timeout & Interrupt (ReentrantLock Killer Feature)

if (lock.tryLock(3, TimeUnit.SECONDS)) {
    try {
        // business logic
    } finally {
        lock.unlock();
    }
} else {
    // timeout handling
}

Impossible with synchronized — critical for production deadlock avoidance.

Multiple Conditions (Solves "Thundering Herd")

synchronized: notify() wakes random waiter; notifyAll() wakes all — unnecessary context switches (thundering herd).

ReentrantLock: Create multiple Condition objects, each with own wait queue → precise wakeup.

Condition notFull = lock.newCondition();
Condition notEmpty = lock.newCondition();
notFull.await();
notEmpty.signal();

Production Selection Guidelines

Prefer synchronized When

Most ordinary concurrency (singleton, simple thread-safe methods).

Code simplicity paramount; avoid manual lock management.

Low-to-moderate contention (JVM-optimized performance matches ReentrantLock).

Debugging convenience needed ( jstack shows lock holder directly).

Must Use ReentrantLock When

Need fair lock (rare).

Need timeout acquisition to avoid deadlock (distributed task scheduling, resource contention).

Need interrupt response for cancellable tasks.

Need multiple conditions for precise wakeup (producer-consumer, thread pools).

Need try-lock or timeout abandonment.

Never Do This

Don't abuse ReentrantLock to "look advanced"! 90% of cases synchronized solves perfectly; manual release easily omitted in finally → deadlock, nightmare to debug.

Interview Differentiators

Cite JDK 1.6 synchronized optimizations: biased, lightweight, spinning, adaptive spinning, elision, coarsening — proves you know performance gap is tiny.

Briefly explain AQS core: state, CLH double-linked queue, CAS — shows bottom-up understanding.

Tie to real business scenario: "Our distributed task scheduler used tryLock to prevent multiple nodes executing same task, effectively avoiding deadlock."

Objectively state drawbacks: synchronized = limited features; ReentrantLock = verbose, error-prone.

Use synchronized whenever possible; only reach for ReentrantLock when synchronized falls short.

AQS (AbstractQueuedSynchronizer) Core Principles & Design

AQS is the absolute cornerstone of java.util.concurrent — ReentrantLock, CountDownLatch, Semaphore, CyclicBarrier all build on it. It's a general-purpose sync framework solving the hardest concurrency problems: thread queuing, wait/wake, lock state management.

AQS is a skeleton for building locks/synchronizers. It encapsulates "queue-and-block" grunt work; we only implement template methods like tryAcquire / tryRelease to produce ReentrantLock, Semaphore, CountDownLatch, etc.

Core Design Philosophy

One sentence: Use a volatile int for sync state, plus a FIFO double-linked wait queue, to manage all threads waiting for the lock.

Three pillars: State, Queue, Methods.

Analogy — "parking lot": state = remaining spaces (0=full, 1=taken, >1=reentrancy count); wait queue = cars queued outside; acquire = grab space (CAS on state); release = drive away (update state, notify next car); block/wake = driver kills engine / attendant waves you in.

Core Data Structures

state — volatile int; sync state representation; volatile visibility, CAS atomic updates, subclass defines meaning (exclusive/shared).

CLH double-linked wait queue — Node nodes forming doubly-linked list; holds all threads failing to acquire; head = current lock holder; tail = last queued; each Node: thread ref, wait status, prev/next pointers.

Node Core Statuses

Node {
    waitStatus  // SIGNAL(-1), CANCELLED(1), CONDITION(-2), PROPAGATE(-3)
    prev / next
    thread
}
SIGNAL(-1)

: Successor needs wakeup by current node. CANCELLED(1): Node cancelled (timeout/interrupt). CONDITION(-2): Node waiting in condition queue. PROPAGATE(-3): Shared mode — wakeup must propagate downstream.

Template Method Pattern

AQS's brilliance: all generic queuing/wait/wake logic frozen; subclasses implement only 5 simple protected methods to create completely different synchronizers: tryAcquire(int arg) — attempt exclusive lock (exclusive mode). tryRelease(int arg) — attempt exclusive release (exclusive mode). tryAcquireShared(int arg) — attempt shared lock (shared mode). tryReleaseShared(int arg) — attempt shared release (shared mode). isHeldExclusively() — current thread holds exclusive lock? (exclusive mode).

Examples: ReentrantLock implements tryAcquire/tryRelease; state = recursion count. CountDownLatch implements tryAcquireShared/tryReleaseShared; state = count. Semaphore implements tryAcquireShared/tryReleaseShared; state = permits.

Queuing Essence: Spin Twice + park Sleep

Key: If predecessor is head, get one more tryAcquire chance — avoids needless park/unpark context switches, a performance win.

Release Flow & Wake Successor

Release calls subclass tryRelease; on success (state→0), unparks head's successor; woken thread re-enters acquire spin loop.

Why LockSupport.park/unpark over wait/notify?

No synchronized block required — more flexible.

Can unpark before park (thread won't block).

Supports interrupt response, no InterruptedException thrown.

Condition Condition Queue

AQS also has a single-linked condition queue cooperating with CLH queue: await(): Wrap current thread into node, add to condition queue, release lock, then park. signal(): Move condition queue head to CLH queue tail; after predecessors release, it wakes naturally.

Enables precise wakeup, unlike synchronized 's notifyAll thundering herd.

Common AQS Implementations Comparison

ReentrantLock — exclusive; state = lock recursion count; core use: reentrant mutex.

ReentrantReadWriteLock — exclusive + shared; state high 16 bits = read count, low 16 = write count; core use: read-write separation.

CountDownLatch — shared; state = remaining count; core use: one thread waits for many.

Semaphore — shared; state = remaining permits; core use: limit concurrent access.

CyclicBarrier — shared; state = remaining waiting threads; core use: multiple threads rendezvous.

Shared mode's PROPAGATE state: on head release, wakeup cascades through all shared nodes — prevents "resources available but threads stay asleep" bugs.

Core Flow 1: Exclusive Lock Acquire (ReentrantLock Example)

1. Thread calls lock() → AQS acquire(1)
2. Try lock: call subclass tryAcquire(1)
   ✅ Success: return, thread holds lock
   ❌ Fail: proceed
3. Wrap thread as Node, CAS-append to queue tail
4. Spin loop:
   a. Check if predecessor is head
   b. If yes: call tryAcquire(1) again
      ✅ Success: set self as new head, remove old head
      ❌ Fail: proceed
   c. If no/failed: LockSupport.park() block self
5. On wake, return to step 4 spin

Why Spin-Check Predecessor-Is-Head?

To reduce block/wake overhead. Kernel context-switch costly; if predecessor just released, spin grabs lock directly without kernel transition.

Core Flow 2: Exclusive Release

1. Thread calls unlock() → AQS release(1)
2. Call subclass tryRelease(1) modify state
   ✅ Success (state→0): proceed
   ❌ Fail (reentrant not fully released): return
3. Locate head's successor
4. LockSupport.unpark() wake successor's thread
5. Woken thread returns to acquire step 4 spin

One-Liner Design Essence

AQS is like a bank's queue-and-call system: state = teller availability / window count; CLH queue = waiting area; grab ticket, glance if only one ahead (head), if so try window again, else sit (park); Condition = special business needs separate waiting room; condition met → brought back to main queue.

Interview Differentiators

CLH variant: AQS uses doubly-linked list (not original singly), adds cancelled-node handling.

Dummy head node: Head is empty sentinel — avoids frequent head creation/deletion, reduces CAS.

Cancelled node cleanup: Timeout/interrupt sets node to CANCELLED, then removes from queue.

Shared-mode propagation: Release cascades wakeup to all shared waiters.

Performance edge: User-space CAS replaces kernel mutex; much faster under low contention.

CAS Principle, ABA Problem & Solution (AtomicStampedReference)

CAS Core Principle (Compare-And-Swap)

CAS is a hardware-level atomic operation, foundation of Java lock-free concurrency; all java.util.concurrent.atomic classes rely on it.

CAS = Compare And Swap

Think of it as an atomic "check-then-replace":

I want to change variable from A to B
① Read memory value V
② Compare V == expected A
③ Yes → swap to B ✅
   No  → do nothing, retry ❌

Backed by CPU instruction (e.g., cmpxchg); Java calls via Unsafe; user-facing = Atomic series like AtomicInteger. atomicInt.compareAndSet(expect, update); Pros: No locks, no thread blocking, great under high-contention lightweight scenarios. Cons: CPU-spinning waste, single-variable atomicity only, and the ABA problem.

Core Three Elements

Memory address V: heap address of variable.

Expected old value A: value expected before update.

New value B: target value.

Atomic Execution Logic

Only if V strictly equals A, atomically update V to B; else fail, return current V.

Essence: Optimistic locking — assume no conflict, attempt update, on failure spin-retry, avoiding block overhead.

Java Implementation Chain

AtomicInteger.getAndIncrement()
→ Unsafe.getAndAddInt()
→ Unsafe.compareAndSwapInt() (native)
→ CPU cmpxchg instruction (x86)

Hardware instruction guarantees atomicity; no OS kernel involvement.

CAS vs Synchronized Comparison

CAS (optimistic): lock-free, spin-retry; no context switch; excellent no-contention perf; high-contention perf degrades due to spin; atomicity scope: single variable; typical flaws: ABA, spin overhead.

Synchronized (pessimistic): heavyweight lock, block threads; context switch yes; lock acquisition overhead; stable high-contention perf; atomicity scope: entire code block; typical flaws: deadlock, context switch.

CAS Fatal Flaw: ABA Problem

Analogy: You have bubble tea (value A), leave for straw, return — tea looks same but someone drank & refilled — that's ABA.

Problem Timeline

T1: Thread 1 reads V=A, prepares CAS; memory V=A.

T2: Thread 1 descheduled; Thread 2 preempts, changes V A→B; memory V=B.

T3: Thread 2 changes V B→A; memory V=A.

T4: Thread 1 resumes, CAS(V,A,C) succeeds; memory V=C.

Core: CAS only compares final value equality, ignores intermediate mutation history. Thread 1 thinks variable untouched; actually Thread 2 mutated it twice.

ABA Essence
Compares "value", but cannot
sense "state changed"

Typical Damage Scenarios

Linked-list/stack node ops → lost nodes, memory leaks, duplicate inserts.

Financial transfers → duplicate deductions/transfers.

State-dependent business → logic unaware of tampering.

Ultimate Fix: AtomicStampedReference

Core Idea

Attach an integer version stamp to each variable; every modification must also update stamp. CAS compares both value and stamp — only if both match exactly, update proceeds.

Structure Diagram

AtomicStampedReference
Pair (reference, stamp)
Ref → Object A, stamp=0
CAS(expectedRef A, newRef B, expectedStamp 0, newStamp 1)
Pair (reference, stamp)
Ref → Object B, stamp=1

Even if value reverts to A, stamp advanced from 0 to 2 — cannot match expected 0; CAS fails, ABA eliminated.

Old vs New CAS Logic

Plain CAS: compare value only; detects ABA? no; use case: simple counters, no state deps.

AtomicStampedReference: compare value + version; detects ABA? yes; use case: complex state-dependent logic.

Core API Example

AtomicStampedReference<String> asr = new AtomicStampedReference<>("A", 1);
boolean success1 = asr.compareAndSet("A", "B", 1, 2); // true
asr.compareAndSet("B", "A", 2, 3); // simulate ABA
boolean success2 = asr.compareAndSet("A", "C", 1, 4); // false

Supplement: AtomicMarkableReference

Simplified AtomicStampedReference; uses boolean mark instead of int stamp.

Only distinguishes "modified or not", cannot count modifications.

Suits scenarios needing only tamper-detection (e.g., GC marking), not history tracking.

Three siblings at a glance: AtomicInteger / AtomicReference — single-variable CAS; prevents ABA? no. AtomicStampedReference — (ref + int version) atomic ops; prevents ABA? yes. AtomicMarkableReference — (ref + boolean mark) atomic ops; prevents ABA? only prevents "one" change.

Choose by scenario: simple counters → AtomicInteger; need exact modification count → AtomicStampedReference; only need "touched?" → AtomicMarkableReference.

Interview Probes

Q: Does AtomicStampedReference solve ALL ABA problems?

A: No. It only solves value-cycling ABA. If referenced object's internal state mutates, AtomicStampedReference detects nothing — it compares reference address, not object internals.

ThreadLocal Principle, Memory Leak & Fix

ThreadLocal Core Principle

ThreadLocal doesn't solve shared-variable concurrency; it provides thread-local variables, giving each thread its own independent copy — achieving "thread isolation".

Principle: Thread → ThreadLocalMap → Entry

Thread-1          Thread-2          Thread-3
  |                 |                 |
  v                 v                 v
ThreadLocalMap  ThreadLocalMap  ThreadLocalMap
  |                 |                 |
  v                 v                 v
Entry[] table   Entry[] table   Entry[] table

Each Thread holds a threadLocals field of type ThreadLocal.ThreadLocalMap. ThreadLocalMap is a static inner class of ThreadLocal, internally an Entry array. Entry extends WeakReference<ThreadLocal<?>> — key is weak reference to ThreadLocal; value is our variable copy (strong reference).

One-liner: Data lives in Thread's own Map; ThreadLocal is just the key. get() fetches current thread's ThreadLocalMap, then uses this as key to hash-find value.

public T get() {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t); // t.threadLocals
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
        if (e != null) return (T)e.value;
    }
    return setInitialValue();
}

Design is clever: thread isolation, lock-free, high performance.

Internal Data Structure

Thread.currentThread() → Thread object
    ↳ threadLocals: ThreadLocalMap (Thread field)
        ↳ Entry[] table (hash table)
            ↳ Entry(key: ThreadLocal<?> (weak ref), value: Object (strong ref))

Core Method Flows

set(T value): 1. Get current thread; 2. Get thread's ThreadLocalMap; 3. Store value with current ThreadLocal object as key.

get(): 1. Get current thread; 2. Get thread's ThreadLocalMap; 3. Lookup value with current ThreadLocal object as key; 4. If absent, call initialValue().

remove(): 1. Get current thread's ThreadLocalMap; 2. Remove Entry keyed by current ThreadLocal.

Key: Data resides in Thread object; ThreadLocal is merely a tool — stores nothing itself.

Memory Leak Deep-Dive

What Is Memory Leak?

Unused objects unreachable by GC, memory grows continuously → OOM.

ThreadLocal Leak Root Cause

ThreadLocalMap.Entry key is weak reference, value is strong reference!

Full reference chain:

Thread → ThreadLocalMap → Entry(key: WeakReference<ThreadLocal>, value: Object)

Leak Formation Step-by-Step

Normal: ThreadLocal has external strong ref → key alive → Entry alive.

Leak Trigger: ThreadLocal external strong ref set to null → key becomes weak ref.

GC Occurs: Weak key collected → Entry key becomes null.

Leak Forms: Value still strongly referenced by Entry → as long as thread lives, value never collected.

Disaster Amplifies: With thread pools, threads reused → "zombie Entries" accumulate endlessly.

Why Weak Key?

A trade-off design:

If key strong: Even after ThreadLocal external ref nulled, key stays → Entry never cleaned → worse leak.

If key weak: At least GC can reclaim key; subsequent get/set/remove proactively purge null-key Entries.

Conclusion: Weak reference mitigates, not solves leak. True leak source is value's strong reference.

Leak Solutions

Golden Rule (Mandatory!)

Always explicitly call remove() after ThreadLocal use!

try {
    threadLocal.set(value);
    // business logic
} finally {
    threadLocal.remove(); // Clean up regardless of exception
}

Auxiliary Measures

Declare ThreadLocal private static: Extends ThreadLocal lifecycle, reduces create/destroy frequency, lowers leak chance.

Avoid large objects: If unavoidable, ensure timely cleanup.

Upgrade JDK: JDK 8+ optimized ThreadLocalMap cleanup: get() / set() trigger heuristic cleanup expungeStaleEntry() on null-key dirty Entries — nulls values, clears slots. But cleanup not full; depends on subsequent get/set calls. If thread never touches ThreadLocal again, leak persists.

Interview Extras

ThreadLocalMap hash collision resolution: Uses linear probing, not HashMap's chaining+tree.

Resize trigger: At 2/3 load factor, doubles capacity.

Dirty Entry cleanup timing: get/set/remove trigger partial cleanup; resize triggers full cleanup.

InheritableThreadLocal: Allows child threads to inherit parent's ThreadLocal values — same leak issues apply.

Common Misconceptions

Myth 1: ThreadLocal causes leaks → don't use. Correct: Proper usage with explicit remove() = safe.

Myth 2: ThreadLocal solves thread-safety. Correct: It provides thread isolation to avoid shared-variable safety issues.

Myth 3: ThreadLocal's value is weak ref. Correct: Only key is weak; value is strong — that's the leak source.

Virtual Threads: Platform vs Virtual Thread Scheduling Differences, Use Cases

Soul Code Demo

HTTP service calls three downstreams (user, order, inventory), each 100ms, synchronous blocking:

// Traditional platform thread pool
ExecutorService pool = Executors.newFixedThreadPool(200);
// High concurrency: 200 threads all stuck in IO wait; new requests rejected

// Virtual thread mode
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    // Tens of thousands concurrency handled easily; each task a virtual thread, code still synchronous
}

This demonstrates the fundamental scheduling model difference.

Core Essence: Platform vs Virtual Threads

Essence: Platform thread = OS thread (KLT) thin wrapper; Virtual thread = JVM-managed lightweight user-mode thread.

Mapping: Platform 1:1 to kernel thread; Virtual M:N to platform threads (user-space scheduling).

Scheduler: Platform = OS kernel; Virtual = JVM scheduler (default ForkJoinPool).

Context switch: Platform = kernel-mode, ~1-5μs; Virtual = user-mode, ~tens of ns.

Max count: Platform = thousands (kernel limit); Virtual = millions (memory-bound).

Stack memory: Platform ~1MB each; Virtual ~few hundred bytes each.

Blocking cost: Platform blocks entire OS thread, high switch cost; Virtual on block "unmounts" carrier thread, yields to other virtual threads — near-zero switch cost.

Create/destroy cost: Platform high (kernel syscall); Virtual minimal (pure user-space).

One-liner: Platform threads are "heavy" OS resources; virtual threads are "light" JVM resources — essentially pausable/resumable code segments.

Scheduling Mechanism Core Differences

Scheduling Panorama

[Platform Thread Scheduling - 1:1 Model]
Platform 1 (OS thread)  Platform 2 (OS thread)  Platform 3 (OS thread)
Blocked 😴              Blocked 😴              Running 🏃
Each thread a "heavy" kernel scheduling entity;
Block = kernel context switch, costly; count capped by OS memory/scheduler overhead.

[Virtual Thread Scheduling - M:N Model]
JVM Scheduler (ForkJoinPool)
Carrier 1 (OS thread)  Carrier 2 (OS thread)
mount |       | unmount, mount another VT
VT-1 🏃♂️(on)  VT-2 😴block  VT-3 🏃♂️(on)  VT-4 ⏳wait  VT-5 ⏳wait ... millions
When VT-2 hits blocking IO, JVM auto "unmounts" VT-2 from carrier,
swiftly mounts VT-5. Blocking instant "thread" switch,
OS thread barely idles.

Core scheduling points:

Virtual threads cannot run alone — must mount on a carrier (platform) thread.

On blocking IO (socket read/write, Thread.sleep, LockSupport.park), JVM triggers unmount: saves virtual thread stack frames to heap, carrier thread immediately runs next ready virtual thread.

When blocking completes, virtual thread re-scheduled onto any free carrier — programmer unaware, code remains synchronous sequential.

Platform Thread Scheduling Flow

App Code → Platform Thread → OS Kernel → CPU Core

OS kernel fully controls scheduling, time-slice round-robin.

Every switch requires kernel transition, save/restore full CPU context (registers, PC, etc.).

Once platform thread blocks (IO, sleep), entire thread suspended — CPU wasted.

Virtual Thread Scheduling Flow (Core Advantage)

App Code → Virtual Thread → JVM Scheduler → Platform Thread → OS Kernel → CPU Core

M:N mapping: Many virtual threads share few platform threads (default = CPU cores).

Mount/Unmount: On blocking, JVM auto-saves execution context (Continuation), unmounts from platform thread, lets it run other ready virtual threads.

On unblock, virtual thread re-mounted on any free platform thread.

Entire scheduling in user-space — no OS involvement, cost negligible.

Key difference: Platform thread block wastes CPU; virtual thread block only pauses itself, platform thread continues work. Why virtual threads support millions of concurrent tasks.

Golden Use Cases & Absolute Avoidances

Golden Scenarios (Use Freely)

All IO-bound tasks: DB CRUD, Redis calls, HTTP/microservice RPC, file I/O.

High-concurrency short tasks: API gateways, message consumers, scheduled jobs, crawlers.

Long call chains: Microservice request fanning out to multiple downstreams.

Absolute Avoidances (Never Use)

CPU-bound tasks: Big data compute, encryption, image processing (virtual threads only concurrency, not parallelism — adds scheduling overhead).

Long-held exclusive locks: Pins carrier thread, starves other virtual threads.

Heavy ThreadLocal storage: Millions of virtual threads → memory explosion.

Interview Must-Know Pitfalls (90% Trip)

Never pool virtual threads! Designed for "use-and-discard"; pooling caps concurrency, defeats purpose.

Avoid >10ms CPU-heavy ops in virtual threads — pins carrier, starves others.

Prefer JDK 21 ScopedValue over ThreadLocal — designed for virtual threads.

Don't use synchronized in virtual threads — pins carrier, prevents unmount; use ReentrantLock instead.

Virtual Threads = "Synchronous Programming Spring" for High-Concurrency IO — minimal resources pull platform threads "out of IO waiting room", one OS thread serves tens/hundreds. Platform threads remain CPU-bound workhorses.

Structured Concurrency: JEP 525, Replaces CompletableFuture Composition

Structured Concurrency is a revolutionary concurrency paradigm previewed in Java 21, finalized in Java 23. It completely solves CompletableFuture's long-standing "orphan threads", "error-handling hell", and "cancellation propagation failure" — arguably the most important Java concurrency advance in a decade.

Scenario: Restaurant Ordering Mini-Program

"User places order" method concurrently executes 3 steps: check user balance, check item stock, calculate freight.

Old Paradigm — CompletableFuture "Scatter Management"

CompletableFuture<BigDecimal> cf1 = CompletableFuture.supplyAsync(() -> checkBalance(id));
CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> checkStock(id));
CompletableFuture<BigDecimal> cf3 = CompletableFuture.supplyAsync(() -> calcFreight(id));
CompletableFuture<Void> combined = CompletableFuture.allOf(cf1, cf2, cf3);
combined.join();
// If cf2 throws (out of stock)… cf1 and cf3 still "streaking" in background, consuming DB connections

Three fatal wounds:

Lifecycle Out of Control: Once supplyAsync throws task into common ForkJoinPool, it's "free". Even if main logic fails, it keeps running, consuming resources.

Exception Propagation Broken: cf2's exception won't auto-cancel cf1/cf3; requires manual handle/whenComplete boilerplate.

Callback Hell: Beyond simple allOf (e.g., any-result-wins, 2-of-3 success), thenApply/thenCompose/thenCombine nest deeply → callback hell.

New Paradigm — Structured Concurrency Core Idea

One principle: "Manage multi-threaded async tasks like writing single-threaded sync code." Parent task forks children; all children must join at parent's join point; scope exit = all children terminated.

Core mechanism: All forked children bound to a StructuredTaskScope. Scope = "container"; when main thread exits scope (via try-with-resources), container forcibly cancels and cleans up all running children.

New Paradigm Code — StructuredTaskScope In Action

try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
    var f1 = scope.fork(() -> checkBalance(id));
    var f2 = scope.fork(() -> checkStock(id));
    var f3 = scope.fork(() -> calcFreight(id));
    scope.join();
    scope.throwIfFailed();
    return new OrderInfo(f1.get(), f2.get(), f3.get());
}

Magic:

ShutdownOnFailure: If f2 (stock check) throws OutOfStockException first, scope immediately interrupts all remaining siblings — no runaway tasks.

try-with-resources scope boundary = cleanup boundary: Method exit (normal or exceptional) guarantees all forked virtual threads terminated — zero thread leaks.

Thread Tree Visualization: In jstack or JFR flame graphs, f1/f2/f3 appear as current main thread's "children"; main blocked on join — crystal clear.

Core Comparison — Fundamental Differences

Programming model: CompletableFuture = reactive/chained callbacks (thenApply); StructuredTaskScope = imperative/sync-blocking (fork + join).

Child cleanup: CompletableFuture = manual (relies on cancel propagation); StructuredTaskScope = automatic (scope exit triggers interrupt).

Failure handling: CompletableFuture = manual handle/whenComplete swallowing; StructuredTaskScope = throwIfFailed auto-cancels all peers, rethrows first failure.

Observability: CompletableFuture = weak (tasks scattered in ForkJoinPool); StructuredTaskScope = strong (parent-child bound, JFR/jstack show tree).

Virtual thread relation: CompletableFuture = compatible but no intrinsic coupling; StructuredTaskScope = deeply bound: each fork runs on new virtual thread.

JEP 525 Evolution & Future

JEP 453 (JDK 21): First preview, core API StructuredTaskScope delivered.

JEP 462/480/499: Iterated previews through JDK 22-25, API matured.

JEP 525 (JDK 26, current target): Sixth preview, adds flexible onTimeout callback — timeout handling from "one-size-fits-all" to programmable.

When Java finally standardizes Structured Concurrency, its combo with virtual threads becomes default server-side paradigm: for IO-heavy fan-out (parallel RPC/DB calls), performance far exceeds thread pools, while code simplicity and maintainability max out.

One-Liner Core Idea

Bind a group of concurrent tasks' lifecycles strictly to the code block that creates them. Like structured programming's "block ends → all variables destroyed", structured concurrency: "block ends → all child tasks guaranteed terminated".

Why Replace CompletableFuture? Pain Points

Thread leaks: CompletableFuture = orphan threads easy (parent fails, children run on); Structured Concurrency = impossible — block exit auto-terminates all children.

Error handling: CompletableFuture = exception chain broken, manual catch-all phases; Structured Concurrency = any child fails → immediate cancel all others.

Cancellation propagation: CompletableFuture = cancel only affects current stage, no recursive child cancel; Structured Concurrency = cancel signal auto-propagates to all children.

Readability: CompletableFuture = nested thenCompose → "callback hell"; Structured Concurrency = sync-style async code, linear reading.

Debugging: CompletableFuture = stack traces broken, can't trace full call chain; Structured Concurrency = preserves complete parent-child call stacks.

Core Execution Model Comparison

CompletableFuture Unstructured:
Main Thread
  launch
Task A          Task B
  |                |
Task A1         Task B1
Main exited, children still running in background → Thread Leak!

Structured Concurrency:
try (var scope = new StructuredTaskScope<>()) {
  Task A      Task B
  scope.join(); // Wait all done
} // Block exit → all children forcibly terminated
Whether normal or exceptional exit, all children guaranteed terminated

Core APIs & Examples

Basic: Wait for All Success

// Traditional CompletableFuture (leak risk)
public User getUserInfo(Long userId) {
    CompletableFuture<User> userFuture = CompletableFuture.supplyAsync(() -> userService.findById(userId));
    CompletableFuture<List<Order>> ordersFuture = CompletableFuture.supplyAsync(() -> orderService.findByUserId(userId));
    return new UserInfo(userFuture.join(), ordersFuture.join());
}

// Structured Concurrency (absolutely safe)
public User getUserInfo(Long userId) throws ExecutionException, InterruptedException {
    try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
        Subtask<User> userTask = scope.fork(() -> userService.findById(userId));
        Subtask<List<Order>> ordersTask = scope.fork(() -> orderService.findByUserId(userId));
        scope.join().throwIfFailed();
        return new UserInfo(userTask.get(), ordersTask.get());
    }
}

Advanced: First Success Wins

try (var scope = new StructuredTaskScope.ShutdownOnSuccess<Product>()) {
    scope.fork(() -> productService.getFromCache(id));
    scope.fork(() -> productService.getFromDB(id));
    scope.fork(() -> productService.getFromRemote(id));
    return scope.join().result();
}

Three Core Guarantees

Lifecycle Binding: Children never outlive parent.

Error Propagation: Child exception auto-propagates to parent, cancels all siblings.

Cancellation Transparency: Parent cancel → all children receive cancel signal.

Applicability & Limits

Best Fits

Microservice fan-out (parallel downstream calls).

Batch data processing (split tasks, parallel execute).

Timeout control (overall timeout → all children auto-terminate).

Not Suitable

Long-lived background tasks (message consumers).

Complex inter-task dependencies.

Scenarios needing manual fine-grained thread lifecycle control.

Structured Concurrency doesn't aim to fully replace CompletableFuture; it offers a safer, more maintainable way to compose concurrent tasks. It elevates concurrency from "manual thread lifecycle management" (low-level) to "declarative task-group management" (high-level), letting Java developers write safe, efficient async code with synchronous-code thinking.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CASVirtual ThreadsThreadLocalJMMStructured ConcurrencyAQSsynchronizedJava Concurrency
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.