Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Concurrency

    Do many things at once, safely — threads, locks, the memory model, executors, CompletableFuture and virtual threads.

    • Module 9 of 11
    • Advanced
    • 12 chapters
    • 1 h 6 min

    Concurrency bugs are the hardest to find and the most asked about in senior interviews. This module makes threads, locks and the memory model concrete.

    What you will be able to do

    • Run work on threads and thread pools
    • Explain visibility, volatile and synchronized
    • Prevent race conditions and deadlocks
    • Build async pipelines with CompletableFuture and virtual threads

    Chapters

    1. Threads in JavaA Thread object represents one thread of execution. Its work is a Runnable — a functional interface with one method, run() — usually5 min2. Thread Pools and ExecutorsA thread pool is a set of reusable worker threads plus a task queue. You submit tasks; free workers pick them up.5 min3. synchronizedEvery Java object has a built-in lock called its monitor (or intrinsic lock).3 min4. The Java Memory Model: Visibility and volatileFor speed, each core has its own caches and registers (01howcomputerswork.md Ch 5–6), and both the JIT compiler and the CPU may7 min5. Atomics and Compare-and-SwapCompare-and-swap (CAS) is a single CPU instruction: "set this memory location to new, but only if it currently holds expected; tell me whether it4 min6. Explicit Locks — and Deadlock in JavaReentrantLock (java.util.concurrent.locks) does what synchronized does, plus:4 min7. Producer–Consumer and BlockingQueueThe producer–consumer pattern: some threads produce work items, others consume them, connected by a queue. It decouples speeds and smooths bursts.4 min8. Concurrent CollectionsHashMap is not thread-safe. Concurrent writes can lose entries, corrupt the bucket structure (07javacollectionsinternals.md Ch 4) and even throw.4 min9. Coordination ToolsCountDownLatch(n) — threads call countDown(); await() blocks until the count reaches 0. One-shot.3 min10. CompletableFuture: Asynchronous PipelinesA CompletableFuture<T> is a Future you can chain: attach what should happen next, without blocking a thread while waiting.4 min11. Virtual Threads (Java 21)A normal Java thread is a platform thread: a thin wrapper around an operating-system thread (02operatingsystems.md Ch 7), with a ~1 MB stack.5 min12. ThreadLocal, Immutability and the Bug Checklistprivate static final ThreadLocal<SimpleDateFormat> FORMAT — 3 min

    Extras