Thread-Safe Class Design
Most concurrency material starts with primitives: here is a mutex, here is a semaphore, here is compare-and-swap. That material already lives in the System Design concurrency section, and this page links to it rather than repeating it. The angle here is different. In a low-level design round you aren't asked to implement a mutex. You're asked to design a ParkingLot, a RateLimiter or a BookingService, and then the interviewer says "now two hundred requests hit it at once". The question becomes: which state does this class own, which rules hold across that state, and how does the class keep those rules true when several threads call it at the same time?
That's a design question, not a primitives question. You answer it the way you answer any other design question: by deciding who owns what, what can change, and where the boundaries are. Locks are one tool among several, and often not the first one you should reach for.
This page covers:
- what "thread-safe" means for a class, and why it's about invariants rather than individual fields;
- the four strategies, in the order you should consider them: don't share, don't mutate, synchronize, delegate;
- choosing between
synchronized,ReentrantLock, atomics and concurrent collections; - lock granularity and lock striping;
- the two bugs that cause most real incidents (check-then-act and read-modify-write), each with a failing demo and a fix;
- visibility, safe publication and
volatile; - documenting a class's thread-safety policy;
- testing concurrent code: stress tests, start gates and jcstress.
Java is the main language because its memory model and library give names to all of these ideas. Python and TypeScript (Node) versions appear where they help, with notes on how their runtimes differ.
Runtime models in one paragraph each
Java runs real OS threads in parallel on all cores (and, since Java 21, cheap virtual threads on top of them). Every shared mutable field is a potential race, and the Java Memory Model decides when one thread's writes become visible to another.
Python (CPython 3.12) has real OS threads, but the Global Interpreter Lock lets only one run Python bytecode at a time. The GIL protects the interpreter's own data structures, not your invariants: the interpreter can switch threads between any two bytecodes that contain a switch point, so a check and the act that follows it can still be split. CPython 3.13 added an optional free-threaded build (PEP 703) without the GIL, so code that leans on the GIL for correctness is a bet on a shrinking guarantee. asyncio is single-threaded and cooperative: a task can only lose control at an await.
Node runs your JavaScript on one thread with an event loop. Two callbacks never run at the same instant, so a synchronous block of code is atomic for free. Races still happen whenever an operation spans an await, because other callbacks run in the gap. True parallelism needs worker_threads; workers share nothing by default and communicate by message, unless you pass a SharedArrayBuffer and coordinate with Atomics.
What thread-safety means for a class
A useful working definition:
A class is thread-safe if it behaves correctly when its methods are called from several threads at once, under any interleaving the runtime might choose, without the callers having to add any synchronization of their own.
"Behaves correctly" means it keeps its invariants (the rules that must hold between its fields whenever no method is in progress) and its postconditions (what each method promises). The last clause matters. If callers must wrap calls in their own lock to use the class safely, then the class isn't thread-safe; it's conditionally thread-safe, and that condition must be written down.
Invariants span fields
The most common misunderstanding is that a class is thread-safe if each of its fields is thread-safe. It isn't. Consider a range with a lower and upper bound, where the invariant is lower <= upper. Make both bounds AtomicIntegers and every individual read and write is atomic. The class is still broken, because the invariant involves both fields and each setter does check-then-act across them.
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
class BrokenRangeDemo {
static volatile NumberRange range = new NumberRange(); // starts as [0, 10]
public static void main(String[] args) throws Exception {
int rounds = 100_000;
AtomicInteger violations = new AtomicInteger();
CyclicBarrier start = new CyclicBarrier(2);
// When both threads finish a round: check the invariant, then reset for the next round.
CyclicBarrier done = new CyclicBarrier(2, () -> {
if (!range.isValid()) violations.incrementAndGet(); // lower 5 > upper 4
range = new NumberRange();
});
Thread a = new Thread(() -> repeat(rounds, start, done, () -> range.trySetLower(5)));
Thread b = new Thread(() -> repeat(rounds, start, done, () -> range.trySetUpper(4)));
a.start(); b.start(); a.join(); b.join();
System.out.println("rounds that broke lower <= upper: " + violations + " of " + rounds);
}
static void repeat(int rounds, CyclicBarrier start, CyclicBarrier done, Runnable action) {
try {
for (int i = 0; i < rounds; i++) { start.await(); action.run(); done.await(); }
} catch (InterruptedException | BrokenBarrierException e) {
throw new IllegalStateException(e);
}
}
}
/** Each field is atomic, but the invariant lower <= upper spans both. Not thread-safe. */
class NumberRange {
private final AtomicInteger lower = new AtomicInteger(0);
private final AtomicInteger upper = new AtomicInteger(10);
boolean trySetLower(int value) {
if (value > upper.get()) return false; // check
lower.set(value); // act: upper may have changed since the check
return true;
}
boolean trySetUpper(int value) {
if (value < lower.get()) return false;
upper.set(value);
return true;
}
boolean isValid() { return lower.get() <= upper.get(); }
}
Each thread checks against the old value of the other bound. Thread A sees upper == 10 and sets lower = 5. Thread B sees lower == 0 and sets upper = 4. Both checks passed, and the range is now [5, 4]. The demo runs the two setters against each other 100,000 times, lined up by a pair of barriers. On our machine (Java 24, 32 hardware threads) five runs printed 1, 4, 1, 2 and 4 broken rounds. An earlier version that started two fresh threads per round printed 0 in all five runs, because thread start-up skew almost never let the two setters overlap. Your numbers will differ, and any given run may print 0, because the window is a few nanoseconds wide. That's exactly why these bugs survive testing.
The fix isn't "more atomics". It's to treat the pair as one unit: guard both bounds with the same lock, or make the pair an immutable object that is replaced as a whole (both fixes appear below). The general rule:
Every invariant that involves several variables must be protected by a single lock, or kept inside a single immutable object, so that no thread can ever observe or act on a half-updated state.
When you design an LLD class, write the invariants down first. For a ParkingLot: "a spot is in exactly one of the free set or the occupied map". For a Wallet: "balance equals the sum of the ledger entries and is never negative". For a Show: "each seat has at most one confirmed booking". Each invariant tells you which fields must change together, and therefore which fields must share a lock.
Degrees of thread-safety
Joshua Bloch's Effective Java (Item 82) names the levels that a class can promise. They're worth using in an interview because they make your intent precise:
| Level | Meaning | JDK examples |
|---|---|---|
| Immutable | State never changes after construction, so no synchronization is needed at all | String, Integer, LocalDate, records with immutable components |
| Unconditionally thread-safe | Mutable, but internally synchronized so that any sequence of calls is safe | AtomicLong, ConcurrentHashMap |
| Conditionally thread-safe | Most methods are safe, but some sequences need external locking | Collections.synchronizedList (iteration must hold the list's lock) |
| Not thread-safe | Callers must synchronize every use, or confine it to one thread | ArrayList, HashMap, StringBuilder |
| Thread-hostile | Unsafe even with external locking, usually because it mutates static state | Rare; usually a bug |
Most classes in a design should sit in the first two rows or the fourth. The fourth is fine as long as the object never escapes a single thread. What you want to avoid is an unconscious mix, where a class looks safe because it uses a concurrent map but has compound operations that aren't.
The four strategies, in order
When a class has state that more than one thread can reach, there are four ways to make it safe. Consider them in this order, because each one is cheaper to reason about than the next:
flowchart TD
Q1{"Does more than one thread<br/>need this state?"} -->|"no"| C["Confine it<br/>(stack, thread, single owner)"]
Q1 -->|"yes"| Q2{"Does it need to change<br/>after construction?"}
Q2 -->|"no"| I["Make it immutable"]
Q2 -->|"yes"| Q3{"Is it one variable or a<br/>standard collection?"}
Q3 -->|"yes"| D["Delegate to an atomic<br/>or concurrent collection"]
Q3 -->|"no, invariants span fields"| L["Synchronize: one lock<br/>per invariant"]
- Don't share (confinement). If only one thread ever touches the state, there's nothing to protect.
- Don't mutate (immutability). Immutable objects can be shared freely.
- Delegate to a class that's already thread-safe: an atomic variable or a concurrent collection, when the state really is a single variable or a single map.
- Synchronize with a lock when invariants span several variables.
Many good designs mix them: an immutable value object, held in an atomic reference, owned by a service whose collections are confined to a single worker thread.
Confinement: don't share
Stack confinement
Local variables live on a thread's own stack, so an object that is created inside a method and never stored anywhere else is invisible to other threads. This is the cheapest safety there is, and the reason a stateless service is automatically thread-safe: a PriceCalculator whose quote(order) method only uses its parameters and locals needs no locks at all. In LLD terms, prefer stateless services that operate on objects passed in over services that stash request data in fields between calls.
The classic mistake is a "helpful" field that's really per-call scratch space:
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
class StackConfinementDemo {
public static void main(String[] args) throws Exception {
System.out.println("shared buffer: wrong labels = " + run(new SharedBufferLabeler()));
int confined = run(new LocalBufferLabeler());
if (confined != 0) throw new AssertionError("stack-confined version must never be wrong");
System.out.println("local buffer: wrong labels = " + confined);
}
static int run(Labeler labeler) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(8);
AtomicInteger wrong = new AtomicInteger();
for (int i = 0; i < 20_000; i++) {
int seat = i;
pool.submit(() -> {
if (!labeler.label(seat).equals("SEAT-" + seat + "-OK")) wrong.incrementAndGet();
});
}
pool.shutdown();
pool.awaitTermination(30, TimeUnit.SECONDS);
return wrong.get();
}
}
interface Labeler { String label(int seat); }
class SharedBufferLabeler implements Labeler {
private final StringBuilder buffer = new StringBuilder(); // per-call scratch stored in a field: a bug
public String label(int seat) {
buffer.setLength(0);
buffer.append("SEAT-").append(seat).append("-OK");
return buffer.toString();
}
}
class LocalBufferLabeler implements Labeler {
public String label(int seat) {
StringBuilder buffer = new StringBuilder(); // stack-confined: invisible to other threads
buffer.append("SEAT-").append(seat).append("-OK");
return buffer.toString();
}
}
Move buffer into the method and the class becomes stateless and safe. In five runs on our machine the shared version produced between 2,664 and 4,278 wrong labels out of 20,000, and the local version produced none. The shared version may also throw from inside StringBuilder when two threads resize it at once; those exceptions vanish into the pool's unread futures here, which is its own lesson.
Thread confinement
Sometimes an object is expensive to create and not thread-safe, so you want one per thread. Java's ThreadLocal, Python's threading.local and similar facilities give each thread its own instance. The textbook example is SimpleDateFormat, which keeps parsing state in fields and corrupts results when shared. The better modern fix is to use an immutable formatter (DateTimeFormatter), which shows the strategy order in action: immutability beats confinement when it's available.
A real case where confinement is the right answer is a database connection. Python's sqlite3 connections refuse by default to be used from a thread other than the one that created them, so a per-thread connection is natural:
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.*;
class ConfinementDemo {
// Not thread-safe: SimpleDateFormat keeps parse state in fields. One copy per thread fixes it.
private static final ThreadLocal<SimpleDateFormat> LEGACY =
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
// Better: DateTimeFormatter is immutable, so one shared instance is safe.
private static final DateTimeFormatter MODERN = DateTimeFormatter.ISO_LOCAL_DATE;
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(8);
List<Future<Boolean>> results = new ArrayList<>();
for (int i = 0; i < 2_000; i++) {
String text = "2026-11-" + String.format("%02d", 1 + i % 28);
results.add(pool.submit(() -> {
String viaLegacy = LEGACY.get().format(LEGACY.get().parse(text));
String viaModern = LocalDate.parse(text, MODERN).format(MODERN);
return viaLegacy.equals(text) && viaModern.equals(text);
}));
}
for (Future<Boolean> f : results) if (!f.get()) throw new AssertionError("corrupted parse");
pool.shutdown();
System.out.println("2000 parses on 8 threads, all correct");
}
}
import sqlite3
import threading
from concurrent.futures import ThreadPoolExecutor
_local = threading.local()
def connection() -> sqlite3.Connection:
# sqlite3 connections are bound to the creating thread by default,
# so each worker thread lazily opens its own.
if not hasattr(_local, "conn"):
_local.conn = sqlite3.connect(":memory:")
return _local.conn
def work(n: int) -> int:
return connection().execute("SELECT ?", (n,)).fetchone()[0]
if __name__ == "__main__":
with ThreadPoolExecutor(max_workers=4) as pool:
results = list(pool.map(work, range(100)))
assert results == list(range(100))
print("100 queries on 4 threads, one connection per thread")
Two warnings. First, in a thread pool, a ThreadLocal lives as long as the pooled thread, so values leak across tasks unless you clear them; with virtual threads (one per task) you may create millions of copies. Second, thread confinement is a convention that the compiler doesn't check. Document it on the field.
Single-owner confinement (the actor and event-loop style)
The most powerful form of confinement in design is to give mutable state one owning thread and let everyone else send it messages. A logger that has a single writer thread draining a queue, a matching engine where one thread owns the order book, and a game loop that owns the world state are all built this way. The shared object is now the queue, which is already thread-safe, and the domain objects behind it don't need a single lock. The actor model is this idea taken all the way, and Concurrency Patterns in LLD shows the producer–consumer version.
Node gives you single-owner confinement by default: the event loop thread owns every object, and workers can only reach it through messages. That's why Node code rarely has locks, and why the races Node does have are about await gaps rather than parallel threads.
Immutability: don't mutate
An immutable object can be shared between any number of threads with no synchronization, because there's nothing to race on. For a Java class to be immutable:
- all fields are
final(records do this for you); - the class can't be subclassed in a way that adds mutable state (
finalclass or record); thisdoesn't escape during construction;- any mutable component, such as a list or array passed in, is defensively copied on the way in and never handed out unwrapped.
The last point is where most "immutable" classes leak. A record with a List<LineItem> component is only as immutable as that list. List.copyOf makes an unmodifiable copy (and returns the same instance if it's already unmodifiable, so it's cheap to apply twice).
Change becomes replacement: instead of order.addItem(x), you write order = order.withItem(x), which returns a new object. That sounds wasteful, but for domain objects of a few dozen fields it is negligible next to a network call, and it removes a whole class of bugs.
import java.util.*;
class ImmutableOrderDemo {
public static void main(String[] args) {
List<LineItem> items = new ArrayList<>(List.of(new LineItem("popcorn", 2, 350)));
Order order = new Order("o-1", items);
items.add(new LineItem("cola", 1, 200)); // caller mutates its own list...
if (order.items().size() != 1) throw new AssertionError("defensive copy missing");
Order bigger = order.withItem(new LineItem("nachos", 1, 400));
if (order.totalCents() != 700 || bigger.totalCents() != 1100) throw new AssertionError();
try { bigger.items().add(new LineItem("x", 1, 1)); throw new AssertionError("mutable!"); }
catch (UnsupportedOperationException expected) { }
System.out.println("original " + order.totalCents() + ", new " + bigger.totalCents());
}
}
record LineItem(String sku, int quantity, long unitCents) {
LineItem {
if (quantity <= 0 || unitCents < 0) throw new IllegalArgumentException("bad line item");
}
long totalCents() { return quantity * unitCents; }
}
/** Immutable: safe to share between threads without any locking. */
record Order(String id, List<LineItem> items) {
Order { items = List.copyOf(items); } // defensive, unmodifiable copy
Order withItem(LineItem item) {
List<LineItem> next = new ArrayList<>(items);
next.add(item);
return new Order(id, next);
}
long totalCents() { return items.stream().mapToLong(LineItem::totalCents).sum(); }
}
from dataclasses import dataclass, replace
@dataclass(frozen=True)
class LineItem:
sku: str
quantity: int
unit_cents: int
def __post_init__(self) -> None:
if self.quantity <= 0 or self.unit_cents < 0:
raise ValueError("bad line item")
def total_cents(self) -> int:
return self.quantity * self.unit_cents
@dataclass(frozen=True)
class Order:
id: str
items: tuple[LineItem, ...] # a tuple, not a list, so the container is immutable too
def with_item(self, item: LineItem) -> "Order":
return replace(self, items=self.items + (item,))
def total_cents(self) -> int:
return sum(i.total_cents() for i in self.items)
if __name__ == "__main__":
order = Order("o-1", (LineItem("popcorn", 2, 350),))
bigger = order.with_item(LineItem("nachos", 1, 400))
assert (order.total_cents(), bigger.total_cents()) == (700, 1100)
try:
order.id = "hacked" # type: ignore[misc]
raise AssertionError("should be frozen")
except AttributeError:
pass
print("original", order.total_cents(), "new", bigger.total_cents())
interface LineItem {
readonly sku: string;
readonly quantity: number;
readonly unitCents: number;
}
class Order {
readonly items: readonly LineItem[];
constructor(readonly id: string, items: readonly LineItem[]) {
// Copy and freeze: readonly is compile-time only, freeze also stops runtime mutation.
this.items = Object.freeze(items.map((i) => Object.freeze({ ...i })));
Object.freeze(this);
}
withItem(item: LineItem): Order {
return new Order(this.id, [...this.items, item]);
}
totalCents(): number {
return this.items.reduce((sum, i) => sum + i.quantity * i.unitCents, 0);
}
}
const order = new Order("o-1", [{ sku: "popcorn", quantity: 2, unitCents: 350 }]);
const bigger = order.withItem({ sku: "nachos", quantity: 1, unitCents: 400 });
if (order.totalCents() !== 700 || bigger.totalCents() !== 1100) throw new Error("wrong totals");
if (!Object.isFrozen(order.items)) throw new Error("items should be frozen");
console.log("original", order.totalCents(), "new", bigger.totalCents());
In TypeScript, readonly is a compile-time promise only, and Object.freeze is shallow. In a single-threaded Node process immutability is less about thread-safety and more about not having one async flow mutate an object that another flow is still using, which is the same bug in a different costume.
Immutable holder plus an atomic reference
Immutability gives a lock-free fix for the broken range from the top of the page. Put both bounds into one immutable object, keep that object in an AtomicReference, and change it with a compare-and-set loop: read the current pair, build a new pair, and install it only if nobody else installed one in the meantime. Because the pair is replaced as a unit, no thread can ever see a half-updated range.
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
class SafeRangeDemo {
public static void main(String[] args) throws Exception {
int violations = 0;
for (int round = 0; round < 20_000; round++) {
SafeRange r = new SafeRange(0, 10);
CountDownLatch go = new CountDownLatch(1);
Thread a = new Thread(() -> { await(go); r.trySetLower(5); });
Thread b = new Thread(() -> { await(go); r.trySetUpper(4); });
a.start(); b.start(); go.countDown(); a.join(); b.join();
Bounds now = r.current();
if (now.lower() > now.upper()) violations++;
}
if (violations != 0) throw new AssertionError(violations + " violations");
System.out.println("20000 rounds, invariant never broken");
}
static void await(CountDownLatch l) {
try { l.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
}
record Bounds(int lower, int upper) {
Bounds { if (lower > upper) throw new IllegalArgumentException(lower + " > " + upper); }
}
/** Both bounds live in one immutable object, replaced atomically as a unit. */
class SafeRange {
private final AtomicReference<Bounds> bounds;
SafeRange(int lower, int upper) { bounds = new AtomicReference<>(new Bounds(lower, upper)); }
boolean trySetLower(int value) {
while (true) {
Bounds old = bounds.get();
if (value > old.upper()) return false; // rejected, nothing changed
if (bounds.compareAndSet(old, new Bounds(value, old.upper()))) return true;
// someone else changed the bounds between get and CAS: re-read and re-check
}
}
boolean trySetUpper(int value) {
while (true) {
Bounds old = bounds.get();
if (value < old.lower()) return false;
if (bounds.compareAndSet(old, new Bounds(old.lower(), value))) return true;
}
}
Bounds current() { return bounds.get(); }
}
import threading
from dataclasses import dataclass
@dataclass(frozen=True)
class Bounds:
lower: int
upper: int
class SafeRange:
"""Python has no user-level CAS, so the immutable pair is swapped under a lock.
Readers can still read self._bounds without the lock: rebinding one attribute
to a new immutable object is atomic in CPython."""
def __init__(self, lower: int, upper: int) -> None:
self._bounds = Bounds(lower, upper)
self._lock = threading.Lock()
def try_set_lower(self, value: int) -> bool:
with self._lock:
if value > self._bounds.upper:
return False
self._bounds = Bounds(value, self._bounds.upper)
return True
def try_set_upper(self, value: int) -> bool:
with self._lock:
if value < self._bounds.lower:
return False
self._bounds = Bounds(self._bounds.lower, value)
return True
def current(self) -> Bounds:
return self._bounds
if __name__ == "__main__":
for _ in range(2_000):
r = SafeRange(0, 10)
gate = threading.Barrier(2)
a = threading.Thread(target=lambda: (gate.wait(), r.try_set_lower(5)))
b = threading.Thread(target=lambda: (gate.wait(), r.try_set_upper(4)))
a.start(); b.start(); a.join(); b.join()
assert r.current().lower <= r.current().upper
print("2000 rounds, invariant never broken")
The CAS loop retries only when there's real contention, so under light load it's very fast. Under heavy contention on one reference, threads waste work rebuilding objects that lose the race; at that point a plain lock is often faster and is always easier to read. The lock-free page covers CAS loops, progress guarantees and the ABA problem in depth.
Choosing a synchronization tool
When state must be shared and mutated, Java gives you four families of tools. They aren't interchangeable; each fits a shape of state.
| Tool | Protects | Strengths | Limits |
|---|---|---|---|
synchronized | Any block of code, any number of fields | Simplest, can't forget to unlock, reentrant, JVM-optimised, visible in thread dumps | No timeout, not interruptible, no fairness, one condition (wait/notify) per object |
ReentrantLock | Same | tryLock() and timed tryLock, lockInterruptibly(), optional fairness, several Conditions | Must unlock() in finally; easy to leak a lock on an exception path |
ReentrantReadWriteLock, StampedLock | Read-mostly state | Many concurrent readers | More overhead than a plain lock when reads are short; see read-write locks |
Atomics (AtomicInteger, AtomicReference, LongAdder) | One variable | Lock-free, very fast for counters and flags, CAS for conditional updates | Can't keep an invariant across two variables (see NumberRange) |
Concurrent collections (ConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue) | One collection | Fine-grained internal locking, atomic per-key operations (putIfAbsent, compute, merge) | Two calls in a row aren't atomic; no atomicity across two maps |
A good rule of thumb: start with synchronized. Move to ReentrantLock when you need something it can't do, typically tryLock with a timeout to avoid deadlock, or interruptible waits so that shutdown can cancel a blocked thread. Before Java 24, a second reason was that a virtual thread blocked inside synchronized pinned its carrier thread; JEP 491 removed that limitation in Java 24, so it no longer drives the choice on current JDKs. Use atomics and concurrent collections when the state really is a single variable or a single map, and for nothing more.
Here is one small domain rule, "decrement stock, but never below zero", written with each tool. The stress test at the bottom fires 10,000 purchases at 1,000 units of stock from 16 threads and checks that exactly 1,000 succeed in each implementation.
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
class StockToolsDemo {
public static void main(String[] args) throws Exception {
List<Stock> impls = List.of(new SyncStock(1000), new LockStock(1000), new AtomicStock(1000));
for (Stock s : impls) {
int sold = hammer(s::tryTake);
if (sold != 1000 || s.remaining() != 0) throw new AssertionError(s + " sold " + sold);
System.out.println(s.getClass().getSimpleName() + ": sold exactly " + sold);
}
StockBySku bySku = new StockBySku(Map.of("A", 1000));
int sold = hammer(() -> bySku.tryTake("A"));
if (sold != 1000) throw new AssertionError("StockBySku sold " + sold);
System.out.println("StockBySku: sold exactly " + sold);
}
interface Attempt { boolean run() throws Exception; }
static int hammer(Attempt attempt) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(16);
AtomicInteger wins = new AtomicInteger();
List<Callable<Void>> tasks = new ArrayList<>();
for (int i = 0; i < 10_000; i++) tasks.add(() -> { if (attempt.run()) wins.incrementAndGet(); return null; });
for (Future<Void> f : pool.invokeAll(tasks)) f.get(); // rethrows any task failure
pool.shutdown();
return wins.get();
}
}
interface Stock { boolean tryTake() throws InterruptedException; int remaining(); }
/** 1. synchronized: the default choice. */
class SyncStock implements Stock {
private int units;
SyncStock(int units) { this.units = units; }
public synchronized boolean tryTake() {
if (units == 0) return false;
units--;
return true;
}
public synchronized int remaining() { return units; }
}
/** 2. ReentrantLock: same rule, plus a bounded wait so a stuck holder can't hang callers. */
class LockStock implements Stock {
private final ReentrantLock lock = new ReentrantLock();
private int units;
LockStock(int units) { this.units = units; }
public boolean tryTake() throws InterruptedException {
if (!lock.tryLock(100, TimeUnit.MILLISECONDS)) return false; // give up instead of blocking forever
try {
if (units == 0) return false;
units--;
return true;
} finally {
lock.unlock(); // always in finally
}
}
public int remaining() { lock.lock(); try { return units; } finally { lock.unlock(); } }
}
/** 3. Atomic: one variable, so a CAS loop is enough and no lock is needed. */
class AtomicStock implements Stock {
private final AtomicInteger units;
AtomicStock(int units) { this.units = new AtomicInteger(units); }
public boolean tryTake() {
while (true) {
int current = units.get();
if (current == 0) return false;
if (units.compareAndSet(current, current - 1)) return true;
}
}
public int remaining() { return units.get(); }
}
/** 4. Concurrent collection: per-key atomic update with computeIfPresent. */
class StockBySku {
private final ConcurrentHashMap<String, Integer> units = new ConcurrentHashMap<>();
StockBySku(Map<String, Integer> initial) { units.putAll(initial); }
boolean tryTake(String sku) {
boolean[] taken = {false};
// The lambda runs atomically for this key: no other update to "sku" can interleave.
units.computeIfPresent(sku, (k, n) -> {
if (n == 0) return 0;
taken[0] = true;
return n - 1;
});
return taken[0];
}
}
The LockStock timeout is a real design decision, not decoration. It returns false both when the stock is empty and when the lock was busy for 100 ms, and a production version would report those differently. In this test the lock is held for nanoseconds, so the timeout never fires; under a pathological stall it would, and the purchase would be refused rather than hung. Whether that's acceptable is a product question, which is exactly the kind of trade-off interviewers want you to say out loud.
Python and Node equivalents
Python has threading.Lock, RLock (reentrant), Condition and Semaphore, and Lock.acquire(timeout=...) plays the role of tryLock. There are no user-level atomic integers; you use a lock, or a structure whose single operations are atomic in CPython (queue.Queue, collections.deque.append/popleft, dict.setdefault). In asyncio you use asyncio.Lock, which only matters around awaits.
In Node, parallel workers can share memory through a SharedArrayBuffer, and the Atomics object provides the same compare-and-swap you saw in AtomicStock. This is the only place in Node where you write genuinely lock-free code, and it's rare in application design, but it shows the model is the same everywhere:
import { Worker } from "node:worker_threads";
// One shared Int32 slot holds the stock. Four workers each try 50,000 purchases.
const shared = new SharedArrayBuffer(4);
const stock = new Int32Array(shared);
stock[0] = 100_000;
const workerSource = `
const { workerData, parentPort } = require("node:worker_threads");
const stock = new Int32Array(workerData);
let wins = 0;
for (let i = 0; i < 50000; i++) {
while (true) {
const current = Atomics.load(stock, 0);
if (current === 0) break;
// compareExchange returns the old value: equal to current means our write won.
if (Atomics.compareExchange(stock, 0, current, current - 1) === current) { wins++; break; }
}
}
parentPort.postMessage(wins);
`;
const results = await Promise.all(
Array.from({ length: 4 }, () =>
new Promise<number>((resolve, reject) => {
const w = new Worker(workerSource, { eval: true, workerData: shared });
w.once("message", resolve);
w.once("error", reject);
}),
),
);
const sold = results.reduce((a, b) => a + b, 0);
if (sold !== 100_000 || stock[0] !== 0) throw new Error(`sold ${sold}, left ${stock[0]}`);
console.log(`4 workers sold exactly ${sold}`, results);
Lock granularity and striping
Once you've decided to lock, the next question is how much state each lock guards. This is the granularity decision, and it's a design trade-off between simplicity and throughput.
Coarse-grained: one lock for the whole object. A ParkingLot with a single synchronized on park and leave is trivially correct: every invariant across all floors is protected. But every car, on every floor, waits for every other car. For an interview, that's usually the right first answer, provided you say what it costs.
Fine-grained: one lock per independent piece of state. If floors never share spots, each Floor can have its own lock, and cars on different floors proceed in parallel. The price is that any operation spanning two floors (moving a car, a global count) now needs two locks, and two locks bring lock ordering and the risk of deadlock.
The rule for choosing: a lock's scope should match an invariant's scope. If an invariant only involves one floor, a floor-level lock suffices. If an invariant spans the whole lot ("total free spots equals the sum of free spots per floor"), either keep a lot-level lock or drop that invariant (compute the total on demand from the floors, accepting that it may be slightly stale).
Lock striping
Per-entity locks have a cost: one lock object per seat, per account or per user can mean millions of locks, created and never freed. Lock striping is the compromise. Keep a fixed array of N locks, and map each key to locks[hash(key) % N]. Two keys in different stripes never contend. Two keys in the same stripe do, but rarely, and memory stays bounded. Guava's Striped does this, and ConcurrentHashMap in Java 7 was built from 16 lock-striped segments. Since Java 8 it locks individual hash bins, which is striping at the finest possible grain.
Striping is a good answer to "what if every request locks the whole AccountService?" Here's a striped account ledger, with a transfer that takes two stripes in a fixed order so that two opposite transfers can't deadlock:
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
class StripedLedgerDemo {
public static void main(String[] args) throws Exception {
StripedLedger ledger = new StripedLedger(16);
for (int i = 0; i < 100; i++) ledger.open("acct-" + i, 1_000);
ExecutorService pool = Executors.newFixedThreadPool(16);
List<Callable<Void>> tasks = new ArrayList<>();
Random seed = new Random(42);
for (int i = 0; i < 50_000; i++) {
String from = "acct-" + seed.nextInt(100), to = "acct-" + seed.nextInt(100);
tasks.add(() -> { ledger.transfer(from, to, 7); return null; });
}
for (Future<Void> f : pool.invokeAll(tasks)) f.get();
pool.shutdown();
long total = ledger.total();
if (total != 100_000) throw new AssertionError("money created or destroyed: " + total);
System.out.println("50000 concurrent transfers, total still " + total);
}
}
/** Balances are guarded by stripe locks: key k is guarded by locks[stripe(k)]. */
class StripedLedger {
private final ReentrantLock[] locks;
private final Map<String, Long> balances = new ConcurrentHashMap<>(); // only the map structure is concurrent
StripedLedger(int stripes) {
locks = new ReentrantLock[stripes];
for (int i = 0; i < stripes; i++) locks[i] = new ReentrantLock();
}
private int stripe(String key) { return Math.floorMod(key.hashCode(), locks.length); }
void open(String account, long cents) { balances.put(account, cents); }
boolean transfer(String from, String to, long cents) {
if (from.equals(to)) return false;
int a = stripe(from), b = stripe(to);
ReentrantLock first = locks[Math.min(a, b)], second = locks[Math.max(a, b)]; // fixed order: no deadlock
first.lock();
try {
second.lock(); // same lock twice is fine: ReentrantLock is reentrant
try {
long fromBalance = balances.get(from);
if (fromBalance < cents) return false;
balances.put(from, fromBalance - cents);
balances.put(to, balances.get(to) + cents);
return true;
} finally { second.unlock(); }
} finally { first.unlock(); }
}
/** Takes every stripe in order, so the total is a consistent snapshot. */
long total() {
for (ReentrantLock l : locks) l.lock();
try { return balances.values().stream().mapToLong(Long::longValue).sum(); }
finally { for (ReentrantLock l : locks) l.unlock(); }
}
}
import random
import threading
from concurrent.futures import ThreadPoolExecutor
class StripedLedger:
def __init__(self, stripes: int) -> None:
self._locks = [threading.Lock() for _ in range(stripes)]
self._balances: dict[str, int] = {}
def _stripe(self, key: str) -> int:
return hash(key) % len(self._locks)
def open(self, account: str, cents: int) -> None:
self._balances[account] = cents
def transfer(self, src: str, dst: str, cents: int) -> bool:
if src == dst:
return False
stripes = sorted({self._stripe(src), self._stripe(dst)}) # lower index first: fixed order
with self._locks[stripes[0]]:
if len(stripes) == 1: # same stripe: Lock isn't reentrant, so take it once
return self._move(src, dst, cents)
with self._locks[stripes[1]]:
return self._move(src, dst, cents)
def _move(self, src: str, dst: str, cents: int) -> bool:
if self._balances[src] < cents:
return False
self._balances[src] -= cents
self._balances[dst] += cents
return True
def total(self) -> int:
for lock in self._locks:
lock.acquire()
try:
return sum(self._balances.values())
finally:
for lock in self._locks:
lock.release()
if __name__ == "__main__":
ledger = StripedLedger(16)
for i in range(100):
ledger.open(f"acct-{i}", 1_000)
rng = random.Random(42)
pairs = [(f"acct-{rng.randrange(100)}", f"acct-{rng.randrange(100)}") for _ in range(20_000)]
with ThreadPoolExecutor(max_workers=16) as pool:
list(pool.map(lambda p: ledger.transfer(p[0], p[1], 7), pairs))
assert ledger.total() == 100_000, ledger.total()
print("20000 concurrent transfers, total still", ledger.total())
Python's threading.Lock isn't reentrant, which is why the Python version takes a single lock when both accounts land in the same stripe. Java's ReentrantLock can simply be taken twice.
Keep critical sections short, and never call out under a lock
Two habits matter as much as the choice of granularity:
- Do the slow work outside the lock. Compute the price, validate the input and build the new object first, then take the lock only to check and swap. A lock held across a network call turns one slow dependency into a queue of blocked threads.
- Don't call unknown code while holding a lock. Listeners, callbacks, strategy objects supplied by the caller and overridable methods are "alien" code: they can block, throw or try to take another lock (and deadlock you). In an observer-style class, copy the listener list under the lock, release it, then notify.
CopyOnWriteArrayListdoes this copy for you and is the standard home for listener lists, because listeners are added rarely and iterated often.
Check-then-act bugs
A check-then-act bug is a decision made on a fact that's no longer true by the time you act on it. "If the username is free, register it." "If the seat is available, book it." "If the cache doesn't have the key, compute and insert it." Each pair of steps is correct alone, and wrong together, because another thread can act in between.
The insidious version uses a thread-safe collection, so the code looks safe. ConcurrentHashMap.containsKey is atomic. put is atomic. The sequence of the two isn't.
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
class CheckThenActDemo {
public static void main(String[] args) throws Exception {
System.out.println("broken: rounds with more than one winner = " + run(new BrokenRegistry()));
int fixed = run(new FixedRegistry());
if (fixed != 0) throw new AssertionError("fixed registry let two users win");
System.out.println("fixed: rounds with more than one winner = " + fixed);
}
/** 2,000 rounds, 8 threads per round, all racing to claim the same username. */
static int run(Registry registry) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(8);
int badRounds = 0;
for (int round = 0; round < 2_000; round++) {
String name = "user" + round;
CountDownLatch go = new CountDownLatch(1);
AtomicInteger winners = new AtomicInteger();
List<Future<?>> fs = new ArrayList<>();
for (int t = 0; t < 8; t++) {
String owner = "owner" + t;
fs.add(pool.submit(() -> {
go.await();
if (registry.register(name, owner)) winners.incrementAndGet();
return null;
}));
}
go.countDown();
for (Future<?> f : fs) f.get();
if (winners.get() > 1) badRounds++;
}
pool.shutdown();
return badRounds;
}
}
interface Registry { boolean register(String username, String owner); }
class BrokenRegistry implements Registry {
private final Map<String, String> owners = new ConcurrentHashMap<>();
public boolean register(String username, String owner) {
if (owners.containsKey(username)) return false; // check
owners.put(username, owner); // act: another thread may have registered in between
return true;
}
}
class FixedRegistry implements Registry {
private final Map<String, String> owners = new ConcurrentHashMap<>();
public boolean register(String username, String owner) {
return owners.putIfAbsent(username, owner) == null; // check and act in one atomic call
}
}
import sys
import threading
class BrokenRegistry:
def __init__(self) -> None:
self._owners: dict[str, str] = {}
def register(self, username: str, owner: str) -> bool:
if username in self._owners: # check
return False
self._normalise(username) # any call is a point where CPython may switch threads
self._owners[username] = owner # act
return True
def _normalise(self, username: str) -> str:
return username.strip().lower()
class FixedRegistry:
def __init__(self) -> None:
self._owners: dict[str, str] = {}
self._lock = threading.Lock()
def register(self, username: str, owner: str) -> bool:
with self._lock: # check and act under one lock
if username in self._owners:
return False
self._owners[username] = owner
return True
def run(registry) -> int:
bad_rounds = 0
for round_no in range(500):
name, wins = f"user{round_no}", []
gate = threading.Barrier(8)
def attempt(owner: str) -> None:
gate.wait()
if registry.register(name, owner):
wins.append(owner)
threads = [threading.Thread(target=attempt, args=(f"owner{t}",)) for t in range(8)]
for t in threads:
t.start()
for t in threads:
t.join()
bad_rounds += len(wins) > 1
return bad_rounds
if __name__ == "__main__":
sys.setswitchinterval(1e-6) # switch threads far more often than the 5 ms default, to expose the race
print("broken: rounds with more than one winner =", run(BrokenRegistry()))
fixed = run(FixedRegistry())
assert fixed == 0
print("fixed: rounds with more than one winner =", fixed)
On our machine, five runs of the Java demo printed between 32 and 43 bad rounds out of 2,000 for the broken registry, and 0 every time for the fixed one. The Python demo (CPython 3.12) printed between 68 and 79 bad rounds out of 500 with the switch interval shrunk to one microsecond, and 0 for the fixed version. With CPython's default 5 ms switch interval, the broken version printed 0 in all three runs we tried, which is the point: a race that doesn't show up in your test is still a race. Since Python 3.10 the interpreter only considers switching threads at certain points (function calls and loop back-edges among them), so a check and an act with no call between them rarely split under the GIL. That's an implementation detail that the free-threaded build discards, not a guarantee to design on.
The fixes, in order of preference:
- Use a single atomic operation that does both steps.
putIfAbsent,computeIfAbsent,compute,merge,replace(key, old, new),AtomicInteger.compareAndSet. In a database: a unique constraint, orINSERT … ON CONFLICT DO NOTHING. - Put the check and the act under the same lock. Necessary when the decision involves several pieces of state.
- Make the act conditional on the check still holding (optimistic concurrency): "set if version is still 7". Booking & Inventory Concurrency builds a whole service on this idea.
A common variant is lazy initialisation: if (cache.get(k) == null) cache.put(k, expensive(k)). Two threads compute the expensive value twice, and one result is thrown away (or worse, two different objects escape and both are used). computeIfAbsent runs the function at most once per key. Don't do slow I/O inside it, though, since it blocks other updates to keys in the same bin.
Check-then-act in Node
A single-threaded runtime avoids the parallel version of this bug, but not the await version. The check and the act are atomic only if no await separates them:
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
class BrokenRegistry {
private owners = new Map<string, string>();
async register(name: string, owner: string): Promise<boolean> {
if (this.owners.has(name)) return false; // check
await sleep(1); // e.g. a profanity-filter call: other requests run here
this.owners.set(name, owner); // act on a stale check
return true;
}
}
class FixedRegistry {
private owners = new Map<string, string>();
private pending = new Map<string, Promise<boolean>>();
async register(name: string, owner: string): Promise<boolean> {
// Claim the name synchronously, before any await, so no other call can slip in.
if (this.owners.has(name) || this.pending.has(name)) return false;
const attempt = (async () => {
await sleep(1);
this.owners.set(name, owner);
return true;
})();
this.pending.set(name, attempt);
try { return await attempt; } finally { this.pending.delete(name); }
}
}
async function race(registry: { register(n: string, o: string): Promise<boolean> }): Promise<number> {
const results = await Promise.all(Array.from({ length: 8 }, (_, i) => registry.register("ada", `owner${i}`)));
return results.filter(Boolean).length;
}
const broken = await race(new BrokenRegistry());
const fixed = await race(new FixedRegistry());
console.log(`broken: ${broken} winners, fixed: ${fixed} winner`);
if (fixed !== 1) throw new Error("fixed registry must have exactly one winner");
This one is deterministic: the broken version always prints 8 winners, because every call passes the check before any of them reaches the set. The fix moves the claim to the synchronous part, before the first await. It's the Node equivalent of putIfAbsent.
Read-modify-write bugs
A read-modify-write bug is a lost update. Two threads read the same value, both compute a new value from it, and both write; the second write silently overwrites the first. count++ is the textbook example, because it compiles to three steps: load, add, store. A view counter, a "seats remaining" field, a wallet balance and a rate limiter's token count all have the same shape.
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
class LostUpdateDemo {
static int plain; // unsafe
static volatile int volatileCount; // still unsafe: volatile is visibility, not atomicity
static final AtomicLong atomic = new AtomicLong(); // safe
static final LongAdder adder = new LongAdder(); // safe, and faster under heavy contention
public static void main(String[] args) throws Exception {
int threads = 8, perThread = 250_000;
CountDownLatch go = new CountDownLatch(1);
Thread[] ts = new Thread[threads];
for (int i = 0; i < threads; i++) {
ts[i] = new Thread(() -> {
try { go.await(); } catch (InterruptedException e) { return; }
for (int j = 0; j < perThread; j++) {
plain++;
volatileCount++;
atomic.incrementAndGet();
adder.increment();
}
});
ts[i].start();
}
go.countDown();
for (Thread t : ts) t.join();
int expected = threads * perThread;
System.out.printf("expected %d | plain %d | volatile %d | atomic %d | adder %d%n",
expected, plain, volatileCount, atomic.get(), adder.sum());
if (atomic.get() != expected || adder.sum() != expected) throw new AssertionError();
}
}
import sys
import threading
class Counter:
def __init__(self) -> None:
self.value = 0
self._lock = threading.Lock()
def unsafe_increment(self) -> None:
current = self.value # read
self._touch() # a call between read and write: a possible switch point
self.value = current + 1 # write, possibly over someone else's update
def safe_increment(self) -> None:
with self._lock:
self.value += 1
def _touch(self) -> None:
pass
def hammer(method_name: str) -> int:
c = Counter()
gate = threading.Barrier(8)
def work() -> None:
gate.wait()
method = getattr(c, method_name)
for _ in range(50_000):
method()
threads = [threading.Thread(target=work) for _ in range(8)]
for t in threads:
t.start()
for t in threads:
t.join()
return c.value
if __name__ == "__main__":
sys.setswitchinterval(1e-6)
print("expected 400000 | unsafe", hammer("unsafe_increment"), "| locked", hammer("safe_increment"))
assert hammer("safe_increment") == 400_000
// In Node the read-modify-write splits across an await, not across threads.
const tick = () => new Promise<void>((r) => setImmediate(r));
class Wallet {
balance = 0;
private queue: Promise<unknown> = Promise.resolve();
async unsafeDeposit(amount: number): Promise<void> {
const current = this.balance; // read
await tick(); // e.g. a fraud check
this.balance = current + amount; // write over any deposit that happened during the await
}
/** A tiny async mutex: each call waits for the previous one, so the sections never overlap. */
safeDeposit(amount: number): Promise<void> {
const run = this.queue.then(async () => {
const current = this.balance;
await tick();
this.balance = current + amount;
});
this.queue = run.catch(() => undefined); // keep the chain alive if one call fails
return run;
}
}
const a = new Wallet();
await Promise.all(Array.from({ length: 100 }, () => a.unsafeDeposit(1)));
const b = new Wallet();
await Promise.all(Array.from({ length: 100 }, () => b.safeDeposit(1)));
console.log(`expected 100 | unsafe ${a.balance} | serialised ${b.balance}`);
if (b.balance !== 100) throw new Error("serialised deposits must all land");
Five runs of the Java demo printed results such as expected 2000000 | plain 1605691 | volatile 1611686 | atomic 2000000 | adder 2000000; the plain and volatile counts changed every run and were always short, by between 9 and 25 percent. The atomic counts were exact every time. With the one-microsecond switch interval, the Python version printed unsafe totals between 162,364 and 168,769 out of 400,000 across five runs, and the locked total was always 400,000. With the default interval, three runs of the unsafe version all came out exact, another reminder that a clean run proves nothing. The TypeScript version is deterministic: the unsafe wallet ends at 1, not 100, because all hundred calls read 0 before any of them writes.
Which fix to choose:
- One counter, increment or add:
AtomicLongor, if many threads update and few read,LongAdder(it spreads updates across internal cells and sums them on read). - One value, arbitrary update:
AtomicReference.updateAndGet(fn)or an explicit CAS loop. The function may run more than once under contention, so it must be side-effect free. - Update that depends on other state: a lock around the read, the computation and the write.
- Value in a map:
merge(key, 1, Integer::sum)orcompute, nevergetfollowed byput. - Value in a database:
UPDATE accounts SET balance = balance + ? WHERE id = ?, which the database executes atomically, rather than reading the balance into the application and writing it back.
Visibility, safe publication and volatile
The two bugs above are about atomicity: steps that should be indivisible but aren't. There's a second, subtler class of bug about visibility: one thread writes a value and another thread never sees it, or sees it in a different order from the one in the source.
This happens because compilers, JITs and CPUs reorder and cache memory operations freely, as long as a single thread can't tell the difference. The Java Memory Model only guarantees that a write is visible to another thread if there's a happens-before relationship between them. The common ways to create one:
- unlocking a monitor or lock happens-before every later lock of the same lock;
- writing a
volatilefield happens-before every later read of that field; Thread.start()happens-before anything in the started thread, and everything in a thread happens-before another thread's successfuljoin()on it;- an action before putting an object into a concurrent collection or
BlockingQueuehappens-before the action after taking it out; - completing a
Futurehappens-beforeget()returning; - a
finalfield set in a constructor is visible to any thread that obtains the object after construction, providedthisdidn't escape.
The classic demonstration is a stop flag. Without volatile, the JIT is allowed to read the flag once, see false, and turn the loop into an infinite one.
class VisibilityDemo {
static boolean plainStop; // no happens-before: the worker may never see the write
static volatile boolean volatileStop; // volatile write happens-before the next volatile read
public static void main(String[] args) throws Exception {
Thread plain = new Thread(() -> { while (!plainStop) { } });
plain.setDaemon(true); // so the JVM can still exit if it spins forever
plain.start();
Thread.sleep(500); // let the JIT compile the hot loop
plainStop = true;
plain.join(1_000);
System.out.println("plain flag: worker stopped = " + !plain.isAlive());
Thread vol = new Thread(() -> { while (!volatileStop) { } });
vol.start();
Thread.sleep(500);
volatileStop = true;
vol.join(1_000);
System.out.println("volatile flag: worker stopped = " + !vol.isAlive());
if (vol.isAlive()) throw new AssertionError("volatile flag must stop the worker");
}
}
On HotSpot (Java 24, x86-64) every one of our five runs printed plain flag: worker stopped = false and volatile flag: worker stopped = true. Nothing in the language promises the plain version will hang; it may stop on a different JVM, a different CPU or with a print statement in the loop body. That's what makes visibility bugs miserable: adding logging to find them often makes them disappear.
What volatile does and doesn't do
volatile gives you visibility and ordering for a single variable: a read always sees the most recent write, and writes made before a volatile write are visible to a thread that reads the volatile and sees that write. It's the right tool for:
- a status flag written by one thread and read by others (
running,shutdownRequested); - publishing a reference to an immutable object (a configuration snapshot that's replaced as a whole);
- the field in correctly written double-checked locking (see Concurrency Patterns in LLD).
It does not make compound operations atomic. volatileCount++ in the lost-update demo lost updates just like the plain field did. If you need "read, decide, write", you need a lock or an atomic.
Safe publication
An object is published when a reference to it becomes reachable by another thread. It's published safely if the other thread is guaranteed to see the object fully constructed. Unsafe publication means another thread can see the reference before it sees the constructor's writes, and observe default values (0, null) in fields that the constructor set.
Safe ways to publish:
- initialise it in a
staticinitialiser (class initialisation is synchronized by the JVM); - store the reference in a
volatilefield or anAtomicReference; - store it in a
finalfield of an object that is itself properly constructed; - store it in a field guarded by a lock that readers also take;
- put it in a thread-safe collection, such as a
ConcurrentHashMaporBlockingQueue.
Immutable objects with final fields get an extra guarantee: even if published through a data race, readers see the final fields correctly initialised. That's one more reason to prefer immutability.
Don't let this escape the constructor
The publication rules assume the object is complete before anyone else sees it. Two common designs break that:
- Registering a listener in the constructor.
eventBus.subscribe(this)(or an inner class or lambda that capturesthis) hands out a reference before the constructor finishes, so an event can arrive on another thread and see half-initialised fields. - Starting a thread in the constructor. The new thread can run before the constructor's remaining lines.
The design fix is a static factory that constructs first and wires afterwards: static Monitor create(EventBus bus) { Monitor m = new Monitor(); bus.subscribe(m); return m; }, plus an explicit start() method for anything that spawns threads. This also makes the class easier to test, since construction no longer has side effects.
Documenting thread-safety
Thread-safety is part of a class's contract, as much as its method signatures, and it can't be inferred from the code alone. A reader can't tell whether a class without locks is unsafe or immutable, or whether a lock is protecting one field or three. Document two things:
- The promise to callers: which degree of thread-safety (immutable, thread-safe, conditionally thread-safe, not thread-safe) and, if conditional, which sequences need which lock.
- The policy for maintainers: which lock guards which field, and which invariants it protects.
The Java Concurrency in Practice annotations @ThreadSafe, @NotThreadSafe, @Immutable and @GuardedBy("lock") are the conventional vocabulary. They're available from the jcip-annotations artifact, and Error Prone's GuardedBy checker can verify @GuardedBy at compile time. The sample declares them locally so that it compiles with nothing but the JDK:
import java.lang.annotation.*;
import java.util.*;
/**
* A waitlist for a sold-out show.
*
* <p><b>Thread-safety:</b> thread-safe. All methods may be called concurrently.
* {@link #snapshot()} returns an immutable copy, so callers may iterate it without locking.
*
* <p><b>Locking policy (for maintainers):</b> {@code queue} and {@code positions} are
* both guarded by {@code this}. Invariant: every user in {@code queue} has exactly one
* entry in {@code positions}, and nobody else does.
*/
@ThreadSafe
class Waitlist {
@GuardedBy("this") private final Deque<String> queue = new ArrayDeque<>();
@GuardedBy("this") private final Set<String> positions = new HashSet<>();
public synchronized boolean join(String user) {
if (!positions.add(user)) return false; // already waiting
queue.addLast(user);
return true;
}
public synchronized Optional<String> next() {
String user = queue.pollFirst();
if (user != null) positions.remove(user);
return Optional.ofNullable(user);
}
public synchronized List<String> snapshot() { return List.copyOf(queue); }
public static void main(String[] args) throws Exception {
Waitlist w = new Waitlist();
Thread[] ts = new Thread[8];
for (int i = 0; i < ts.length; i++) {
ts[i] = new Thread(() -> { for (int u = 0; u < 1_000; u++) w.join("u" + u); });
ts[i].start();
}
for (Thread t : ts) t.join();
if (w.snapshot().size() != 1_000) throw new AssertionError(w.snapshot().size());
System.out.println("8 threads joined the same 1000 users, waitlist size " + w.snapshot().size());
}
}
@Documented @Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS) @interface ThreadSafe { }
@Documented @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.CLASS) @interface GuardedBy { String value(); }
Notice that the class locks on this and says so. Some teams prefer a private lock object (private final Object lock = new Object()) so that outside code can't take the class's lock and cause contention or deadlock. Both are defensible; what matters is that the documentation names the lock. If the class lets clients take its lock for compound actions (as Collections.synchronizedList does for iteration), document that explicitly, because it's now part of the public API and you can't change it later.
In an interview you won't write Javadoc, but you should say the equivalent in one sentence: "Show is thread-safe; the seat map and the hold map are both guarded by the show's lock, because the invariant 'a seat is either free, held or booked' spans both." That sentence earns more credit than a page of locking code.
Testing concurrent code
Concurrency bugs are probabilistic, so tests for them are too. A passing run proves little; a failing run proves a lot. The goal is to make the bad interleaving as likely as possible, and to check the invariant rather than a specific output.
A stress test with start and end gates
Starting threads one after another lets the first one finish before the last one starts, so there's no actual contention. A start gate fixes this: every worker blocks on a CountDownLatch(1) until the main thread releases them all at once. An end gate (CountDownLatch(n)) lets the main thread wait for every worker. Repeat the whole thing many times, use more threads than cores, and check invariants that must hold no matter how the threads interleaved.
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
class StressHarness {
public static void main(String[] args) throws Exception {
int rounds = 200, threads = 32;
for (int round = 0; round < rounds; round++) {
SeatMap seats = new SeatMap(10);
CountDownLatch startGate = new CountDownLatch(1);
CountDownLatch endGate = new CountDownLatch(threads);
AtomicInteger wins = new AtomicInteger();
List<Throwable> errors = Collections.synchronizedList(new ArrayList<>());
for (int t = 0; t < threads; t++) {
String user = "user" + t;
Thread worker = new Thread(() -> {
try {
startGate.await(); // everyone starts together
for (int s = 0; s < 10; s++) if (seats.book(s, user)) wins.incrementAndGet();
} catch (Throwable e) {
errors.add(e);
} finally {
endGate.countDown();
}
});
worker.start();
}
startGate.countDown();
if (!endGate.await(10, TimeUnit.SECONDS)) throw new AssertionError("deadlock or hang in round " + round);
if (!errors.isEmpty()) throw new AssertionError(errors.get(0));
// Invariants, not outputs: exactly one winner per seat, and every seat taken.
if (wins.get() != 10) throw new AssertionError("round " + round + ": " + wins.get() + " wins");
if (seats.bookedCount() != 10) throw new AssertionError("round " + round + ": seat map inconsistent");
}
System.out.println(rounds + " rounds x " + threads + " threads: every seat had exactly one winner");
}
}
class SeatMap {
private final String[] holders;
SeatMap(int size) { holders = new String[size]; }
synchronized boolean book(int seat, String user) {
if (holders[seat] != null) return false;
holders[seat] = user;
return true;
}
synchronized int bookedCount() { return (int) Arrays.stream(holders).filter(Objects::nonNull).count(); }
}
import threading
class SeatMap:
def __init__(self, size: int) -> None:
self._holders: list[str | None] = [None] * size
self._lock = threading.Lock()
def book(self, seat: int, user: str) -> bool:
with self._lock:
if self._holders[seat] is not None:
return False
self._holders[seat] = user
return True
def booked_count(self) -> int:
with self._lock:
return sum(h is not None for h in self._holders)
if __name__ == "__main__":
rounds, n_threads = 100, 32
for round_no in range(rounds):
seats = SeatMap(10)
start_gate = threading.Barrier(n_threads) # nobody starts until everyone is ready
wins: list[int] = []
errors: list[BaseException] = []
def worker(user: str) -> None:
try:
start_gate.wait()
for s in range(10):
if seats.book(s, user):
wins.append(s) # list.append is atomic in CPython
except BaseException as e:
errors.append(e)
threads = [threading.Thread(target=worker, args=(f"user{t}",)) for t in range(n_threads)]
for t in threads:
t.start()
for t in threads:
t.join(timeout=10)
assert not t.is_alive(), "hang"
assert not errors, errors[0]
assert sorted(wins) == list(range(10)), (round_no, wins)
assert seats.booked_count() == 10
print(f"{rounds} rounds x {n_threads} threads: every seat had exactly one winner")
Features of a good concurrency stress test:
- A start gate, so threads really overlap.
- A timeout on the end gate, so a deadlock fails the test instead of hanging the build.
- Errors captured from worker threads. An exception thrown inside a
Threaddoesn't fail the test by itself; it's printed and lost. - Invariant checks ("exactly one winner per seat"), not exact output checks.
- Many rounds and more threads than cores, to make preemption at bad moments likely.
- A negative control. Run the harness against a deliberately broken version once, and confirm it fails. A test that can't catch the bug is worse than no test, because it gives false confidence. The check-then-act and lost-update demos above are exactly that kind of control.
jcstress, and deterministic testing
Stress tests find atomicity bugs well but are poor at finding memory-model bugs, which depend on JIT compilation and CPU reordering. For those, the OpenJDK project provides jcstress, a harness in which you write a tiny class with two or more @Actor methods that run concurrently against shared state, plus an @Arbiter or result object, and declare which outcomes are acceptable, acceptable-but-interesting or forbidden with @Outcome. jcstress then runs the actors billions of times across many JIT and scheduling modes and reports a histogram of observed outcomes. It's how JDK engineers verify that classes like ConcurrentHashMap behave correctly on ARM as well as x86. It needs its own Maven build, so it isn't shown as a runnable sample here, but it's the tool to name when an interviewer asks how you'd test a lock-free class or a publication idiom.
The other end of the spectrum is deterministic testing: inject the source of nondeterminism and control it. Inject a Clock so hold expiry can be tested without sleeping. Inject an Executor so a test can run tasks on the calling thread in a chosen order. Put a hook (a latch or a callback) between the check and the act in a test build, so you can force the bad interleaving every time. The TypeScript examples on this page are deterministic for exactly this reason: an await is an explicit, controllable interleaving point.
Other tools worth naming: thread dumps (jstack, or jcmd <pid> Thread.print) show which thread holds which monitor and detect Java-level deadlocks; Java Flight Recorder records lock contention; ThreadSanitizer finds data races in C, C++ and Go; Python has no race detector, but running tests under the free-threaded 3.13+ build makes latent races far more visible.
How to talk about this in an LLD round
When the interviewer turns to concurrency, a strong answer follows the same shape every time:
- Name the shared state. "The shared state is the seat map in each
Showand the hold map. Requests run on a pool of threads." - Name the invariants. "A seat is in exactly one state. A hold belongs to one user. Stock never goes negative."
- Name the race. "Booking is check-then-act: two requests can both see the seat free."
- Pick the strategy and justify the granularity. "Each
Showgets its own lock, because invariants never span shows. Different shows book in parallel." - Say what you'd do across processes. "With several app servers, the in-memory lock isn't enough, so the database enforces it with a unique constraint or a conditional update."
- Say how you'd test it. "A stress test: 50 threads, one seat, start gate, assert exactly one winner."
The LLD interview framework puts this step after the core code, and Common LLD Mistakes shows the most common failure.
Common mistakes
| Mistake | Why it's wrong | Fix |
|---|---|---|
"I used ConcurrentHashMap, so it's thread-safe." | Individual calls are atomic; sequences aren't | Use putIfAbsent, compute, merge, or a lock around the sequence |
Making every method synchronized | A sequence of synchronized calls still isn't atomic, and alien calls under the lock can deadlock | Protect each invariant's whole operation; call out after releasing |
| Different locks for fields in one invariant | Each lock protects its field, but nothing protects the relationship | One lock per invariant |
volatile counter | Visibility without atomicity; increments are still lost | AtomicLong or LongAdder |
Locking on a public object, a String literal or a boxed Integer | Unrelated code may lock the same object; interned values are shared JVM-wide | A private final lock object |
Forgetting unlock() on an exception path | The lock is held forever | lock(); try { … } finally { unlock(); } |
| Holding a lock across I/O | One slow dependency blocks every thread | Do I/O first, lock only for the check-and-swap |
| Listener registration or thread start in the constructor | this escapes before construction finishes | Static factory plus an explicit start() |
| "Python has the GIL, so no locks needed." | The GIL protects the interpreter, not your invariants, and is optional from 3.13 | Lock compound operations |
| "Node is single-threaded, so no races." | Any await between check and act is a gap | Claim synchronously before awaiting, or serialise with an async mutex |
Interview questions
-
"What does it mean for a class to be thread-safe?" It keeps its invariants and postconditions under any interleaving of concurrent calls, with no extra synchronization required from callers. If callers must lock around some sequences, it's conditionally thread-safe, and that must be documented.
-
"Both fields are
AtomicInteger. Why isn't the class thread-safe?" Because the invariant relates the two fields, and each method checks one and writes the other in separate steps. Atomic fields make each access atomic, not the relationship. Fix it with one lock over both, or one immutable object holding both in anAtomicReference. -
"When would you choose
ReentrantLockoversynchronized?" When you need whatsynchronizedcan't give:tryLockwith a timeout (to avoid deadlock or bound latency), interruptible lock acquisition (for cancellation and clean shutdown), fairness, or several condition queues on one lock. Otherwisesynchronizedis simpler and can't leak a lock. -
"
AtomicLongorLongAdderfor a request counter?"LongAdderif many threads increment and reads are occasional, because it spreads contention across cells.AtomicLongif you need an exact value often or need CAS operations likecompareAndSet. -
"Is
volatileenough for a counter? For a flag?" Not for a counter:++is read-modify-write, and volatile doesn't make it atomic. Yes for a flag written by one thread and read by others, because visibility is all you need. -
"What is safe publication, and how can an object be published unsafely?" Making a reference visible to other threads in a way that guarantees they see the fully constructed object. Unsafe publication, such as assigning to a plain field read by another thread without synchronization, can let a reader see default values in fields the constructor set. Publish through a static initialiser, a volatile or atomic field, a final field, a lock, or a concurrent collection.
-
"Your
ParkingLothas one lock. How would you scale it?" Match lock scope to invariant scope. If floors never share spots, use one lock per floor; use striping if there are many fine-grained entities. Any operation spanning two locks must take them in a fixed global order. Drop or relax global invariants (compute totals on demand) rather than keep a global lock for them. -
"What is lock striping?" A fixed array of N locks, with each key mapped to a lock by hash. It gives most of the parallelism of per-key locks with bounded memory. Keys sharing a stripe contend occasionally; operations needing every key take all stripes in order.
-
"How do you test that
book()never double-books?" A stress test: many rounds, more threads than cores, aCountDownLatchstart gate so threads really overlap, a timeout so deadlocks fail fast, exceptions captured from workers, and an invariant assertion of exactly one winner per seat. Run it against a deliberately broken version to prove it can fail. For memory-model questions, use jcstress. -
"Python has a GIL. Do you still need locks?" Yes, for any compound operation. The GIL serialises bytecode, but threads can switch between the check and the act. It's also gone in the free-threaded build. In asyncio you need
asyncio.Lockonly when a critical section contains anawait. -
"How can a single-threaded Node service double-book?" If the availability check and the booking write are separated by an
await(a database call, a payment call), other requests run in between and see the same stale state. Claim the resource synchronously before the firstawait, use an async mutex per resource, or push the atomic step into the database. -
"Why shouldn't you call a listener while holding a lock?" The listener is code you don't control. It may block, take a long time, throw, or try to acquire another lock that a different thread holds while waiting for yours, which is a deadlock. Copy the listeners under the lock and notify after releasing it.