Layers of Logic

Phase XIV

Concurrency

Threads, shared state, synchronization, atomics, executors, and virtual threads.

11 sections223 min of reading44 exercises

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
  1. 14.1Processes and ThreadsDistinguish processes from threads, identify per-thread and shared state, and observe the background threads present in a Java program.ThreadCore19 min4 exercises
  2. 14.2Creating Threads and Thread StatesCreate work with Runnable and Thread, start it correctly, and interpret all six Thread.State values.Thread statesCore19 min4 exercises
  3. 14.3Interruption and Cooperative CancellationRequest thread cancellation with interruption, handle InterruptedException without losing the request, and coordinate shutdown with join.InterruptionCore19 min4 exercises
  4. 14.4Race Conditions, Atomicity, and VisibilityReproduce lost updates, stale reads, and ordering surprises, then separate the atomicity and visibility guarantees each fix requires.Race conditionVisibilityCore22 min4 exercises
  5. 14.5synchronized and Monitor LocksSelect the correct monitor, define a protected invariant, limit critical-section size, and prevent deadlock with consistent lock ordering.Monitor lockCore21 min4 exercises
  6. 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 notifyCore20 min4 exercises
  7. 14.7Explicit Locks and ConditionsUse ReentrantLock, timed and interruptible acquisition, Condition queues, and ReadWriteLock only when their extra control is required.Explicit locksCore21 min4 exercises
  8. 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-swapCore20 min4 exercises
  9. 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 problemCore19 min4 exercises
  10. 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 FutureCore21 min4 exercises
  11. 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 threadCore22 min4 exercises