Skip to content

Path 3 of 4

Concurrency Foundations

Threads and the Java Memory Model, races, visibility and atomicity, and the tools that fix them: synchronized, volatile, locks and atomics. Then what goes wrong when threads wait on each other.

0 / 6 lessons completed · about 170 min in total

After this path you can

Outcomes

  • Explain a race from the interleaving that causes it
  • Pick between synchronized, volatile, a lock and an atomic
  • Read a thread dump and find a deadlock or a contended lock
  • State what happens-before guarantees and what it does not

Lessons

  1. 1Thread lifecycle and the Java Memory ModelWhat a platform thread costs, the six thread states and how each one looks in a thread dump, interruption, daemon threads, and why one thread may never see another's write.Foundation28 min
  2. 2Race conditions: visibility and atomicityLost updates, check-then-act races and flags that are never seen: two separate problems, how each one looks in production, and why both pass tests and fail under load.Foundation26 min
  3. 3synchronized, volatile and happens-beforeWhat an intrinsic lock guards and what it does not, what volatile adds and what it cannot, the five happens-before rules you actually use, and double-checked locking done wrong and right.Intermediate30 min
  4. 4Locks: ReentrantLock, ReadWriteLock and StampedLockWhat java.util.concurrent.locks adds to synchronized, when read locks and optimistic reads pay off, and how threads waiting for these locks look in a thread dump and in JFR.Intermediate30 min
  5. 5Atomics and compare-and-setHow compare-and-set updates one variable without a lock, what the retry loop costs, where ABA and LongAdder come in, and why two atomics do not make one atomic operation.Intermediate26 min
  6. 6Deadlocks, livelocks, starvation and lock contentionFour ways threads stop each other making progress, how each one looks in a thread dump and in JFR, and the fix that belongs to each: lock order, timeouts with back-off, shorter critical sections.Advanced30 min

Simulations and labs for this path

A simulation in the browser, then the same failure on a real JVM.

  1. Simulation 2Lost updates: counter++ is three steps
  2. Simulation 3Deadlock: two transfers, two locks, opposite order
  3. Simulation 6Lock contention: eight threads, the throughput of one