Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| Thread / Runnable / Callable | a thread / its work / work that returns a value | 1, 2 |
| start vs run | new thread vs plain call | 1 |
| NEW / RUNNABLE / BLOCKED / WAITING / TIMED_WAITING / TERMINATED | thread states | 1 |
| interrupt / InterruptedException | polite stop request / how waiting code receives it | 1 |
| daemon thread | doesn't keep the JVM alive | 1 |
| thread pool / ExecutorService / Future | reusable workers / its interface / a pending result | 2 |
| ThreadPoolExecutor: core / max / queue / rejection | pool tuning parameters | 2 |
| backpressure | slowing producers instead of piling up work | 2, 7 |
| monitor / synchronized | every object's built-in lock / using it | 3 |
| reentrant | the holder can re-acquire the lock | 3 |
| visibility / reordering | seeing other threads' writes / instructions moved for speed | 4 |
| Java Memory Model / happens-before | rules for cross-thread visibility / the guarantee relation | 4 |
| volatile | visibility + ordering for one variable, not atomicity | 4 |
| double-checked locking | lazy init with two checks + volatile | 4 |
| CAS / atomic classes | compare-and-swap / lock-free AtomicInteger etc. | 5 |
| LongAdder / ABA | contention-friendly counter / value changed and changed back | 5 |
| ReentrantLock / tryLock / fairness / Condition | explicit lock / try without waiting / FIFO access / wait-signal | 6 |
| read-write lock | many readers or one writer | 6 |
| jstack | prints thread dumps, detects deadlocks | 6 |
| producer–consumer / BlockingQueue / poison pill | pattern / waiting queue / stop signal | 7 |
| wait / notify / spurious wakeup | low-level waiting / waking / waking for no reason | 7 |
| ConcurrentHashMap / compound atomic ops | thread-safe map / merge, compute, putIfAbsent | 8 |
| check-then-act | non-atomic sequence of safe calls | 8 |
| CopyOnWriteArrayList / ConcurrentSkipListMap | read-mostly list / concurrent sorted map | 8 |
| CountDownLatch / Semaphore / CyclicBarrier | wait for n events / limit concurrency / group rendezvous | 9 |
| CompletableFuture / thenApply / thenCompose / thenCombine / exceptionally / orTimeout | async pipeline pieces | 10 |
| common pool | shared default ForkJoinPool | 10 |
| platform thread / virtual thread / carrier / pinning | OS thread / JVM thread / its host / stuck to the host | 11 |
| ThreadLocal | per-thread copy of a variable | 12 |
Cheat Sheet#
Pick the tool
one flag, many readers volatile
counter / single value AtomicInteger/Long (LongAdder for hot counters)
multi-step invariant synchronized (default) / ReentrantLock (tryLock, timeout, fairness)
mostly reads, rare writes ReadWriteLock / CopyOnWriteArrayList
shared map ConcurrentHashMap + merge / compute / computeIfAbsent
hand work between threads BlockingQueue (bounded)
limit concurrent use Semaphore
wait for n things CountDownLatch
async fan-out + combine CompletableFuture with your own executor + orTimeout
many blocking I/O tasks virtual threads (Java 21)
CPU-bound parallel work fixed pool ≈ coresTemplates
try (var pool = Executors.newFixedThreadPool(n)) { Future<T> f = pool.submit(callable); f.get(); }
lock.lock(); try { ... } finally { lock.unlock(); }
while (true) { int cur = a.get(); if (!ok(cur)) break; if (a.compareAndSet(cur, next(cur))) break; }
sem.acquire(); try { callApi(); } finally { sem.release(); }
synchronized (lock) { while (!condition) lock.wait(); ...; lock.notifyAll(); }
CompletableFuture.supplyAsync(task, executor).thenApply(f).exceptionally(ex -> fallback).orTimeout(2, SECONDS).join();
try (var ex = Executors.newVirtualThreadPerTaskExecutor()) { ex.submit(blockingTask); }Rules
start(), not run() · always shut down executors · bounded queues in production
same lock for all access · unlock in finally · consistent lock order · short critical sections
thread-safe calls don't compose → use compound atomic methods
volatile ≠ atomic · wait() in a while loop · never swallow InterruptedException
timeouts on every external call · custom executor for blocking async work
virtual threads: I/O-bound only, don't pool, semaphore for scarce resources, avoid synchronized+blocking (Java 21)Try This Yourself#
See the invisible flag. Run the plain vs
volatileflag experiment from Ch 4. Then replacevolatilewith reading the flag inside asynchronizedgetter — does the loop stop? Explain with happens-before.Race, then fix it three ways. Eight threads incrementing a shared
int100,000 times each: plain (wrong),synchronized(right),AtomicInteger(right),LongAdder(right). Time all four.Build a rate-limited AI client. Use a
Semaphore(3)and a virtual-thread executor to run 50 fake "AI calls" (sleep(200)) and record the maximum concurrency seen. AddorTimeoutso any call over 500 ms falls back to a cached answer.Deadlock on purpose. Write the two-account transfer from Operating Systems Ch 9 with
synchronized, make it deadlock, runjstack <pid>to see "Found one Java-level deadlock", then fix it with a consistent lock order by account ID.