Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are virtual threads in Java 21 and how do you enable them in Spring Boot?

Virtual threads, final in Java 21 (JEP 444), are lightweight threads managed by the JVM and scheduled onto a small number of platform carrier threads; when a virtual thread blocks on I/O, the JVM unmounts it so the carrier can run another one. That makes thread-per-request cheap, so plain blocking code can scale to many thousands of concurrent requests. In Spring Boot 3.2 and later, spring.threads.virtual.enabled=true switches Tomcat, Jetty, the task executors and other components to virtual threads. They do not make CPU-bound code faster, downstream limits such as connection pools still apply, and pinning on synchronized blocks was a problem until JDK 24 removed most of it (JEP 491).

Why this question is asked

For years Java developers had two choices for high-concurrency services: a large thread pool with blocking code, limited to a few hundred concurrent requests per instance, or reactive programming, which scales well but is harder to write, test and debug. Virtual threads offer a third way: write simple blocking code, and let the JVM make waiting cheap. Interviewers ask about them because they change architecture decisions, and because candidates often overstate what they do. In this answer, we will recap why platform threads limit concurrency, see how virtual threads work, create some by hand, enable them in Spring Boot, and look at their limits, including pinning.

1A Quick Recap: Why Platform Threads Limit Concurrency

A traditional Java thread, now called a platform thread, is a thin wrapper around an operating system thread. Each one reserves a stack, often 1 MB of address space, and the OS scheduler has to manage it. Creating thousands of them is expensive, so servers use pools: Tomcat's default maximum is 200 threads. In the thread-per-request model, each request occupies one thread from start to finish, including the time it spends waiting for a database or a remote service.

That waiting is the problem. If a request spends 5 ms computing and 95 ms waiting on I/O, a pool of 200 threads can only serve about 2,000 requests per second, while the CPU sits mostly idle. Reactive programming solved this by never waiting on a thread, at the cost of a very different programming style.

2How Virtual Threads Work

A virtual thread is a java.lang.Thread that is not tied to an OS thread. The JVM runs virtual threads on a small pool of platform threads called carrier threads, by default one per CPU core, using a ForkJoinPool scheduler. When a virtual thread runs, it is mounted on a carrier. When it reaches a blocking operation such as a socket read, the JDK's I/O code parks it instead of blocking the carrier: its stack is saved to the heap, it is unmounted, and the carrier picks up another virtual thread. When the data arrives, the virtual thread is scheduled again, possibly on a different carrier, and continues exactly where it stopped.

Because a parked virtual thread is only a small object on the heap, the JVM can hold hundreds of thousands of them. So thread-per-request becomes cheap again: every request can have its own thread, and blocking code that waits on I/O no longer wastes a scarce resource.

Step throughA virtual thread blocking on a database call
Virtual thread ACarrier thread 1DatabaseVirtual thread B
mount A, run request code
send query, read socket
park: stack saved, unmount
mount B, run another request
result ready, A scheduled again
mount A (any carrier), continue after the read

6 messages in order. Play them, or step through them one at a time.

The carrier never waits; only the virtual thread does, and waiting is cheap.

3Creating Virtual Threads by Hand

The API is ordinary Thread code, which is the whole point: existing blocking code runs unchanged.

Java
Thread t = Thread.ofVirtual().name("worker-1").start(() -> System.out.println("hello"));
t.join(); // hello
System.out.println(t.isVirtual()); // true
try (ExecutorService ex = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10_000; i++) {
ex.submit(() -> { Thread.sleep(Duration.ofSeconds(1)); return null; });
}
} // all 10,000 tasks finish in about 1 second

Notice the second example. Ten thousand tasks each sleep for one second, and the whole block completes in about one second, because each task has its own virtual thread and sleeping unmounts it. With a fixed pool of 200 platform threads, the same work would take about 50 seconds. Virtual threads are not pooled: we create one per task and let it finish, because creating one is cheap.

4Enabling Them in Spring Boot

In Spring Boot 3.2 and later, a single property switches the application to virtual threads, on Java 21 or newer.

Properties
# application.properties
spring.threads.virtual.enabled=true
Platform threads compared with virtual threads
AspectPlatform threadVirtual thread
Backed byone OS threadJVM object, mounted on a carrier
Typical counthundredshundreds of thousands
Cost of blocking on I/Oholds an OS threadunmounts, frees the carrier
Poolingyes, pools of about 200no, one per task
CPU-bound worksame speedsame speed, no gain
Code styleblockingthe same blocking code

With it, Tomcat and Jetty handle each request on a new virtual thread instead of a pooled platform thread. The auto-configured task executor used by @Async and the scheduler behind @Scheduled use virtual threads, and so do several integrations, such as Spring for Apache Kafka and RabbitMQ listener containers; check your version for the exact list. Our controllers, services and JDBC repositories stay the same blocking code. One practical detail: virtual threads are daemon threads, so an application whose only work is scheduled tasks may need spring.main.keep-alive=true to keep the JVM running.

5What Virtual Threads Do Not Do

Virtual threads make waiting cheap; they do not make computing faster. A CPU-bound task, such as image resizing or a big calculation, still needs a core, and with only 8 cores, 8 such tasks are the limit regardless of thread type. Virtual threads also do not remove downstream limits. If a thousand virtual threads all want a database connection and HikariCP has 10, 990 of them wait for the pool, and the database itself can only do so much work. Concurrency limits now have to be explicit, with semaphores or pool sizes, rather than implied by a thread pool.

There is also pinning. A virtual thread is pinned when it cannot unmount while blocked, so it holds its carrier. Before JDK 24, blocking inside a synchronized block or method pinned the virtual thread, which could exhaust the few carriers and stall the application; the advice was to replace synchronized with ReentrantLock around blocking calls. JDK 24 removed most of this pinning (JEP 491), so on Java 25 synchronized is generally fine. Pinning can still happen when a virtual thread blocks inside native code, for example through JNI. ThreadLocals still work, but because there can be millions of threads, caching large objects in ThreadLocals is a bad idea.

6Follow-up questions

Should we pool virtual threads?

No. Pooling exists because platform threads are expensive to create; virtual threads are cheap, and a pool would only add a limit we do not want. To limit concurrency, for example calls to a fragile service, use a Semaphore around the call instead of a small pool.

Do virtual threads replace reactive programming?

For many request-response services, yes in practice: they give similar concurrency with simpler code and ordinary debugging. Reactive still has strengths in streaming with backpressure and in composing many asynchronous sources, so it remains a good fit for some services.

How do I see pinning on older JDKs?

On JDK 21 to 23, the JFR event jdk.VirtualThreadPinned records pinned threads with stack traces, and the system property jdk.tracePinnedThreads printed them; that property was removed in JDK 24 along with most of the pinning. JFR remains the tool to use for the cases that still pin.

7Conclusion

In this answer, we saw that virtual threads are lightweight threads that the JVM schedules onto a few carrier threads, unmounting them when they block on I/O, so thread-per-request becomes cheap and blocking code scales to many concurrent requests. We created them with Thread.ofVirtual() and Executors.newVirtualThreadPerTaskExecutor(), and enabled them in Spring Boot 3.2 and later with spring.threads.virtual.enabled=true for Tomcat, Jetty, task executors and more. We also saw their limits: no speedup for CPU-bound code, connection pools and other downstream limits still apply, and pinning on synchronized blocks mattered until JDK 24 removed most of it.

What to say in the interviewAbout 90 seconds aloud

Open with

Virtual threads, final since Java 21, are threads managed by the JVM rather than one-to-one by the operating system. The JVM runs them on a small pool of platform carrier threads, about one per core.

Then, if they want more

When a virtual thread blocks on I/O, like a socket read for a JDBC query, the JDK parks it, saves its stack to the heap and unmounts it, so the carrier runs another virtual thread; when the data arrives, it is mounted again and continues.

Since a parked virtual thread is just a small heap object, we can have hundreds of thousands, so thread-per-request becomes cheap and ordinary blocking code scales to many concurrent requests. We create them with Thread.ofVirtual or Executors.newVirtualThreadPerTaskExecutor, and we do not pool them.

In Spring Boot 3.2 and later on Java 21 or newer, setting spring.threads.virtual.enabled to true makes Tomcat or Jetty use a virtual thread per request, and switches the task executor, the scheduler and several listener containers too.

I would add the caveats: they do not speed up CPU-bound work, and downstream limits still apply, so a HikariCP pool of 10 is still 10 connections and concurrency limits must be explicit.

And pinning: before JDK 24, blocking inside synchronized pinned the carrier, so we used ReentrantLock; JEP 491 in JDK 24 removed most of that, though native calls can still pin.

About this answer

Last updated
12 November 2026
Versions it names
Java 24, Java 21, Spring Boot 3.2, Java 25
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are virtual threads in Java 21 and how do you enable them in Spring Boot?": https://scrivio-interview-library.netlify.app/spring-boot/reactive-testing-and-ops/57a6acb3e2/