Phase XIV
Concurrency
Threads, shared state, synchronization, atomics, executors, and virtual threads.
Create and stop threads cooperatively, then reproduce atomicity, visibility, and ordering failures over shared state. Apply monitor locks, conditions, explicit locks, atomic variables, executors, CompletableFuture, and virtual threads to the problems each tool is designed to solve.
When you finish this phase you can
- Explain which memory and execution state a thread owns and which state it shares
- Explain a race condition precisely enough to point at the two instructions
- Explain the visibility guarantees of volatile and the atomicity it does not provide
- Use synchronized correctly and identify the selected monitor object
- Choose between a lock, an atomic and a queue for a given problem
- Explain compare and swap, and the retry loop built on it
- Use an executor to separate task submission from thread creation and scheduling
- 14.1Processes and ThreadsDistinguish processes from threads, identify per-thread and shared state, and observe the background threads present in a Java program.Thread
- 14.2Creating Threads and Thread StatesCreate work with Runnable and Thread, start it correctly, and interpret all six Thread.State values.Thread states
- 14.3Interruption and Cooperative CancellationRequest thread cancellation with interruption, handle InterruptedException without losing the request, and coordinate shutdown with join.Interruption
- 14.4Race Conditions, Atomicity, and VisibilityReproduce lost updates, stale reads, and ordering surprises, then separate the atomicity and visibility guarantees each fix requires.Race conditionVisibility
- 14.5synchronized and Monitor LocksSelect the correct monitor, define a protected invariant, limit critical-section size, and prevent deadlock with consistent lock ordering.Monitor lock
- 14.6wait, notify, and notifyAllCoordinate threads with a condition predicate, call wait while holding the monitor, and recheck the predicate after every wake-up.wait and notify
- 14.7Explicit Locks and ConditionsUse ReentrantLock, timed and interruptible acquisition, Condition queues, and ReadWriteLock only when their extra control is required.Explicit locks
- 14.8Atomic Variables and Compare-and-SwapUse atomic variables for single-value updates, implement a compare-and-swap retry loop, and recognise when an invariant still requires a lock.Compare-and-swap
- 14.9CAS Retry Loops and the ABA ProblemExplain failed compare-and-swap retries, measure repeated work under contention, and detect state that changed and later returned to the same value.The ABA problem
- 14.10Executors, Thread Pools, and FuturesSubmit Runnable and Callable tasks, retrieve results and failures through Future, size a thread pool, and choose a bounded task queue.Executor and Future
- 14.11CompletableFuture and Virtual ThreadsCompose asynchronous results without blocking, propagate failures through a pipeline, and use virtual threads for large numbers of blocking tasks.CompletableFutureVirtual thread