TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

All concepts ยท java ยท concurrency ยท jvm

Virtual threads

Lightweight threads managed by the JVM (Java 21+). When one blocks on I/O, it's parked and its carrier OS thread serves others, so simple blocking code can handle huge numbers of concurrent requests.

9 min3 animations

๐Ÿ“š Builds on๐Ÿ–Š๏ธ Threads, locks and race conditions

๐Ÿ–จ๏ธ Cheat sheet

A waiter per table

In this restaurant every table gets its own waiter, who stands beside it the whole time the food is cooking. Waiters are expensive, so there are only so many. On a busy night, most waiters are standing still, and new guests can't be served.

The thread-per-request limit

Classic Java servers give each request a platform thread, which is an OS thread: about a megabyte of stack, and costly to create and switch. Pools are capped at a few hundred. Most of those threads spend their time blocked on I/O, waiting for a database or another service, so throughput hits the thread limit long before the CPU is busy.

Reactive and CompletableFuture code works around this, at the cost of harder-to-read code and stack traces.

One waiter per table3 steps
๐Ÿช‘Table 1๐Ÿช‘Table 2๐Ÿช‘Table 3๐ŸคตWaiters๐ŸณKitchen
Press play and watch it happen!

Pin it to the pass

Now every table's order is just a slip, which costs almost nothing. A few waiters carry slips around. When food is cooking, the waiter pins the slip to the kitchen pass and serves someone else. When it's ready, any free waiter picks it up.

Mounting and unmounting

A virtual thread is a Thread managed by the JVM. It runs on a small pool of carrier threads (about one per core). When it blocks on I/O, the JVM unmounts it, saving its stack on the heap, and the carrier runs another. When the I/O finishes, it's remounted on any carrier.

1try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {2    for (var order : orders) executor.submit(() -> ship(order));   // plain blocking code3}

In Spring Boot 3.2+, spring.threads.virtual.enabled=true runs request handling on virtual threads.

Waiters who don't stand around3 steps
๐Ÿ“Table 1's order๐Ÿ“Table 2's order๐Ÿ“Table 3's order๐ŸคตA few waiters๐ŸณKitchen
Press play and watch it happen!

Where it doesn't help

More slips don't make the cooking faster: if the work is the waiter's own effort, there are still only a few waiters. And if the kitchen only handles ten orders at a time, limit that with a ticket system, not by hiring fewer waiters.

Caveats

  • Virtual threads speed up waiting, not computing. CPU-bound work is still limited by cores.
  • Pinning: native calls (and, before JDK 24, blocking inside synchronized) keep the carrier busy.
  • Don't pool them; create one per task. Limit scarce resources with a Semaphore or connection pool.
  • Be careful with large ThreadLocal caches, since there may be millions of threads.
When a waiter gets stuck3 steps
๐Ÿ“An order๐ŸคตWaiter๐ŸณKitchen
Press play and watch it happen!

๐Ÿงฉ Quick quiz

Quick quiz! 3 questions about Virtual threads, told the same way as the story.
  1. On a busy night, why can't new guests be served?
  2. What does a waiter do while the food is cooking?
  3. The kitchen can only handle ten orders at a time. What should you do?
Answer every question to check.Passing marks Virtual threads as done.
Got the hang of Virtual threads? Mark it as done, and your paths show how far youโ€™ve got.

Saved in this browser.

โ˜• Java concurrencyStep 4 of 5

โ† Previous on the path๐Ÿ“Ÿ CompletableFuture and asyncNext on the path โ†’๐Ÿงน Garbage collection