Concurrency and virtual threads in Java
You write safe parallel code with executors, CompletableFuture, locks and concurrent collections, and manage thousands of tasks simply with JDK 21 virtual threads.
Level 301 · 18 lessons · 15 practice labs
Lessons
Foundations
- The thread lifecycle and interruption
- Visibility, volatile and the Java memory model
- ReentrantLock, ReadWriteLock and tryLock
- Atomic variables and LongAdder
- Short task: money transfers that do not deadlock
High-level tools
- Executor pools and the right size
- Chaining and combining with CompletableFuture
- Concurrent collections and BlockingQueue
- CountDownLatch, Semaphore and CyclicBarrier
- Divide and conquer with Fork/Join
- Short task: a tool that indexes files in parallel
Modern concurrency
- Virtual threads: thousands of blocking tasks
- Pinning and virtual-thread pitfalls
- Structured concurrency and scoped values
- Timeouts and cancellation
- Diagnosing deadlocks and stalls with jstack
- Short task: fan out and collect a hundred requests with virtual threads
Capstone
- Capstone: a bounded, cancellable file-processing queue
Practice labs
- Writing a worker that honours interruption: Turn a worker that swallows InterruptedException into one that cleans up and exits on interruption while preserving the interrupt flag.
- Fixing a visibility bug with volatile: Solve the visibility problem of a thread that never sees its stop flag and spins forever with volatile and show the reason with a test.
- Preventing publication of a half-built object: Fix a class that leaks its this reference in the constructor and gets double-checked lazy initialisation wrong, following safe publication rules.
- Fixing lost increments in a counter: Replace an int counter that undercounts under concurrent increments with AtomicLong and compare with LongAdder under heavy writes.
- Guarding a read-heavy table with a ReadWriteLock: Protect a price table with a ReadWriteLock, release the lock in finally and test that readers never see a half update.
- Money transfers that do not deadlock: Prevent deadlock in Yelkovan Bankası's mutual transfers with lock ordering or timed tryLock and show the total balance is preserved under the load test.
- Shutting down an ExecutorService properly: Fix a program that never ends because it does not shut its pool down, using shutdown and awaitTermination.
- Combining two services with CompletableFuture: Run price and stock lookups concurrently and combine them, falling back to a default if one fails and applying a timeout.
- Choosing pool size and rejection policy: Rebuild a pool that exhausts memory through an unbounded queue with a bounded queue and a suitable rejection policy, choosing size by measurement for CPU and I/O work.
- Waiting for all services to be ready: Wait for three start-up tasks to finish with a CountDownLatch and report which one did not if time runs out.
- Limiting concurrent calls to an external service: Allow at most five calls at once with a Semaphore and test that the permit is returned on exception and the limit holds.
- Indexing files in parallel with Fork/Join: Split a directory tree with RecursiveTask and merge the word index, choose the threshold by measurement and show the result equals the sequential version.
- Fanning out a hundred requests with virtual threads: Run a hundred fake blocking requests with a virtual-thread-per-task executor and collect the results.
- Cancelling remaining tasks on a timeout: Run three subtasks with structured concurrency and show by test that the others are cancelled when one fails or time runs out.
- Diagnosing a stall with jstack: Find the deadlock and the virtual threads pinned inside synchronized from the thread dump of a service that stalls under load, fix them and pass the load test.
About this course
You write safe parallel code with executors, CompletableFuture, locks and concurrent collections, and manage thousands of tasks simply with JDK 21 virtual threads.
What you will learn
- Foundations
- High-level tools
- Modern concurrency
- Capstone
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.