Virtual threads
On this page 8 topics
Overview
Topics
- Project Loom
- virtual threads
- pinning considerations
- blocking I/O
Learn first
2.5 h of practice (estimate)
Learning outcomes
- Explain how virtual threads differ from platform threads and why they suit blocking I/O.
- Enable virtual threads in Spring Boot and measure the effect.
- Identify pinning and other cases where virtual threads do not help (CPU-bound work, limited downstream resources).
- Explain why resource limits (connection pools, semaphores) still matter.
Lessons
Lab and exercises
Lab
MID-007-LAB1 · Load comparison
Outcomes: 1–4 · Time: ~2.5 h Run Shop's slow-supplier endpoint under identical load with platform threads and with virtual threads. Record throughput, latency percentiles, and database pool saturation. Add a limit (semaphore or pool size) and re-run. Acceptance: results explained, including the new bottleneck.
Exercises (assessment: scenario quiz on when virtual threads help)
| ID | Outcome | Task |
|---|---|---|
| MID-007-EX01 | 1 | Explain in five sentences how a virtual thread differs from a platform thread and what happens when it blocks. |
| MID-007-EX02 | 1 | Run 10,000 tasks that sleep 1 s on a fixed pool of 200 and on a virtual-thread-per-task executor; compare times. |
| MID-007-EX03 | 2 | Enable virtual threads in Shop and prove via a log of Thread.currentThread() that requests run on virtual threads. |
| MID-007-EX04 | 3 | For four workloads (JDBC-heavy, blocking HTTP, image processing, file hashing), say whether virtual threads help. |
| MID-007-EX05 | 4 | Add a Semaphore(20) to protect a downstream client and show requests queueing safely. |
| MID-007-EX06 | 3 | Describe how you would detect pinning in a running service. |
Show solutions
- EX01 Virtual threads are JVM-managed and cheap; they mount on carrier threads and unmount when blocked on supported I/O, freeing the carrier.
- EX02 The pool takes about 10,000/200 x 1 s = 50 s; virtual threads take about 1-2 s.
- EX03 Log
Thread.currentThread().isVirtual()in a controller; it prints true when enabled. - EX04 JDBC-heavy: helps up to the pool limit. Blocking HTTP: helps. Image processing and hashing: CPU-bound, no gain.
- EX05 Acquire before the call, release in
finally; excess threads wait cheaply. - EX06 Use JFR's virtual-thread pinned event or the tracing flag (verify for the JDK in use), then check for blocking inside
synchronizedor native code. - Outcome 2's measurement is also covered by the lab.
Quiz
Outcomes: O1 how they work · O2 Spring Boot and measuring · O3 pinning and limits · O4 resource limits.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises MID-007-EX01 to EX06 (including workload scenarios on when virtual threads help) plus the lab. Pass: at least five exercises correct and the lab explaining the new bottleneck.
Common mistakes
Pooling virtual threads; expecting CPU-bound speed-ups; removing all concurrency limits.
Assessment
Scenario quiz: when virtual threads help.
Career relevance
Modern Java service design.