Skip to content
Ameen Abozaid
MID-007

Virtual threads

  • 6 Lessons
  • 2.5 h practice (estimate)
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

  1. Explain how virtual threads differ from platform threads and why they suit blocking I/O.
  2. Enable virtual threads in Spring Boot and measure the effect.
  3. Identify pinning and other cases where virtual threads do not help (CPU-bound work, limited downstream resources).
  4. Explain why resource limits (connection pools, semaphores) still matter.

Lessons

  1. 01The thread-per-request problem
  2. 02Virtual threads explained
  3. 03Virtual threads in Spring Boot
  4. 04Pinning and other pitfalls
  5. 05Structured concurrency (preview status)
  6. 06Demo: platform vs virtual threads under load

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)

IDOutcomeTask
MID-007-EX011Explain in five sentences how a virtual thread differs from a platform thread and what happens when it blocks.
MID-007-EX021Run 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-EX032Enable virtual threads in Shop and prove via a log of Thread.currentThread() that requests run on virtual threads.
MID-007-EX043For four workloads (JDBC-heavy, blocking HTTP, image processing, file hashing), say whether virtual threads help.
MID-007-EX054Add a Semaphore(20) to protect a downstream client and show requests queueing safely.
MID-007-EX063Describe 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 synchronized or 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.

  1. 1A virtual thread blocked on supported I/O:O1
  2. 2Virtual threads are best for:O1
  3. 3You should create virtual threads:O1
  4. 4The Spring Boot property to enable them is:O2
  5. 5After enabling them you should:O2
  6. 6Pinning means:O3
  7. 7A CPU-bound endpoint on virtual threads will:O3
  8. 8The core idea of structured concurrency is:O1
  9. 9With 10,000 virtual threads and a 10-connection pool, the bottleneck is:O4
  10. 10To protect a limited downstream service you should:O4

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.

Type to search levels, phases, modules and lessons.