Section 10 · Stage 3: Master Java
Multithreading & Concurrency
Threads, synchronisation, executors, concurrent collections and CompletableFuture, with the classic concurrency bugs and their fixes.
What you will be able to do
- Create and control threads and explain the life cycle of a thread
- Find race conditions and fix them with synchronized, locks or atomics
- Run tasks with executors, thread pools and virtual threads
- Use concurrent collections and synchronizers instead of building your own
- Chain asynchronous work with CompletableFuture
- Recognise deadlocks and other classic concurrency bugs and fix them
What you will learn
10.1Thread Basics9 lessons
What threads are, why programs use them and how to start, pause, wait for and stop them.
- What is a thread?30 min
- Create a thread with Thread and Runnable35 min
- start() versus run()30 min
- Why thread output changes every run30 min
- Pause and wait: sleep and join35 min
- Stop a thread politely with interrupt35 min
- Thread states and daemon threads35 min
- What happens when a thread throws35 min
- Thread basics: common mistakes and practice45 min
10.2Race Conditions & synchronized8 lessons
See shared data go wrong with two threads and fix it with locking, then design a class that stays correct under many threads.
10.3Visibility & volatile7 lessons
Why one thread may not see another thread's change, and the rules that guarantee it does.
10.4Thread Coordination7 lessons
Make threads wait for each other, from wait and notify to blocking queues, and stop workers cleanly.
10.5Locks & Atomic Variables9 lessons
More flexible tools than synchronized, and lock-free counters built on compare-and-set.
- ReentrantLock and unlock in finally35 min
- tryLock, timeouts and fair locks35 min
- Condition objects: waiting with a Lock40 min
- synchronized versus ReentrantLock30 min
- ReadWriteLock for read-heavy data40 min
- AtomicInteger and AtomicLong35 min
- Compare-and-set: how atomics work40 min
- LongAdder for busy counters35 min
- Locks and atomics: common mistakes and practice50 min
10.6Executors & Thread Pools9 lessons
Hand tasks to a pool instead of creating threads by hand, collect results, size the pool and use virtual threads in Java 21.
- Why use a thread pool?30 min
- ExecutorService: submit and execute35 min
- Callable and Future40 min
- invokeAll and invokeAny35 min
- Run tasks later and repeatedly35 min
- Shut down a pool correctly35 min
- Choose a pool size and configure ThreadPoolExecutor45 min
- Virtual threads (Java 21)45 min
- Executors: common mistakes and practice50 min
10.7Concurrent Collections8 lessons
Thread-safe collections, why wrapping a normal collection is often not enough, and how ConcurrentHashMap works inside.
- Normal collections under many threads35 min
- Collections.synchronizedList and its limits35 min
- ConcurrentHashMap basics35 min
- ConcurrentHashMap atomic methods40 min
- How ConcurrentHashMap works inside40 min
- CopyOnWriteArrayList35 min
- Blocking queues compared40 min
- Concurrent collections: common mistakes and practice45 min
10.8Synchronizers6 lessons
Ready-made tools to make groups of threads wait or limit how many run at once.
10.9CompletableFuture8 lessons
Run work in the background and combine results without blocking, with error handling, timeouts and your own executor.
- Why CompletableFuture?30 min
- supplyAsync and runAsync35 min
- thenApply, thenAccept and thenRun35 min
- thenCompose versus thenApply35 min
- Combine tasks: thenCombine, allOf and anyOf40 min
- Handle errors: exceptionally, handle and whenComplete40 min
- Your own executor and timeouts40 min
- CompletableFuture: common mistakes and practice50 min
10.10Classic Concurrency Bugs8 lessons
Deadlocks, livelocks and other bugs that only show up sometimes, how to find and fix them, and the interview questions built on them.
10.11Concurrency Mini Projects5 lessons
Small programs that put threads, pools, locks, concurrent collections and CompletableFuture to work.
