Sub topic 5
Turning on virtual threads in Spring Boot
This chapter assumes you have a Spring Boot 3.2 or later MVC application on Tomcat or Jetty, running on Java 21 or newer. It also assumes you know what a virtual thread is, as covered in the previous chapters, and that you can edit application.properties or application.yml. By the end you will have switched virtual threads on, checked that the switch worked, and learned the common ways it fails silently.
The one-line switch
Spring Boot 3.2 added a single property that moves the framework's thread usage onto virtual threads:
spring.threads.virtual.enabled=trueThe YAML form is equivalent:
spring:
threads:
virtual:
enabled: trueThe property defaults to false. Boot reads it through a condition (@ConditionalOnThreading) that is true only when two things hold: the property is true and the JVM is Java 21 or newer. If either is false, Boot keeps its platform-thread configuration and does not report an error.
You change no controller, service, or repository code. Your blocking calls, such as JDBC queries and RestClient requests, stay as they are. Each request simply runs on a virtual thread instead of a pooled platform thread.
What the property changes
The property does not alter your code. It swaps the executors that Boot creates for you.
| Component | Without the property | With the property |
|---|---|---|
| Tomcat request handling | Pool of platform threads, capped by `server.tomcat.threads.max` | A new virtual thread per request |
| Jetty request handling | Jetty's `QueuedThreadPool` of platform threads | Same pool, with virtual threads running the requests |
| `applicationTaskExecutor` (used by `@Async` and MVC async support) | `ThreadPoolTaskExecutor` with a bounded pool | `SimpleAsyncTaskExecutor` that starts a virtual thread per task |
| `@Scheduled` task scheduler | `ThreadPoolTaskScheduler` | `SimpleAsyncTaskScheduler` using virtual threads |
Boot also switches some messaging listener executors, depending on the version and the libraries on your classpath. Check the release notes for the Boot version you run rather than assuming.
Undertow and other servers
Boot 3.2 does not support virtual threads for Undertow, and the property has no effect there. Use Tomcat (the default) or Jetty. If you have swapped in Undertow for historical reasons, change servers before you start the rollout.
Verifying that it worked
Never assume the property took effect. Add a small endpoint that reports the thread handling the request:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ThreadInfoController {
@GetMapping("/thread")
String thread() {
Thread current = Thread.currentThread();
return "virtual=" + current.isVirtual() + ", thread=" + current;
}
}Start the application and call it:
curl http://localhost:8080/threadWith the property on, the response looks like this (the thread number and worker name will differ):
virtual=true, thread=VirtualThread[#47,tomcat-handler-0]/runnable@ForkJoinPool-1-worker-1Read the output in three parts:
virtual=trueconfirms the request runs on a virtual thread.tomcat-handler-0is the thread name Boot gives to Tomcat's virtual threads. With platform threads you would see names such ashttp-nio-8080-exec-1instead.ForkJoinPool-1-worker-1is the carrier thread, a platform thread from the JDK's scheduler. The carrier's name is not stable, because the JDK may run the same virtual thread on different carriers over its lifetime.
If you see virtual=false, work through the checks in the troubleshooting section below.
Extending virtual threads to your own executors
The auto-configured applicationTaskExecutor is created only when you have not defined your own executor bean. If your code already declares a ThreadPoolTaskExecutor, the property does not touch it, and that pool keeps its platform threads and its size limit.
Spring Framework 6.1 added a switch on SimpleAsyncTaskExecutor so you can create virtual-thread executors yourself:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
@Configuration
class AsyncConfig {
@Bean
SimpleAsyncTaskExecutor reportExecutor() {
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("report-");
executor.setVirtualThreads(true);
return executor;
}
}Tasks submitted to this executor run on virtual threads named with the report- prefix. This executor does not pool threads and has no concurrency limit unless you call setConcurrencyLimit. That is useful for I/O work. It is dangerous for work that fans out into a scarce resource, which a later chapter covers.
Behavior that changes without any code change
Turning on the property changes more than the thread type. Four effects are worth knowing before you ship.
- Request concurrency is no longer capped by
server.tomcat.threads.max. Tomcat's default of 200 request threads stops limiting you. Other limits still apply:server.tomcat.max-connections(8192 by default) bounds open connections, andserver.tomcat.accept-countbounds the queue of waiting connections. - Downstream pools become the bottleneck. If 2,000 requests are in flight and your Hikari connection pool holds 10 connections, 1,990 virtual threads wait for a connection. They wait cheaply, but they still wait, and requests may time out. The thread pool used to hide this mismatch by limiting how many requests could reach the database at once.
- Threads are no longer reused. Each task gets a fresh virtual thread, so a
ThreadLocalvalue never leaks to the next request through pool reuse. It also means expensive objects cached in aThreadLocalare rebuilt for every request. - Thread names are less useful. Virtual threads have an empty name by default. Boot gives names to the ones it creates (such as
tomcat-handler-0), but threads you create elsewhere may appear unnamed in logs.
Troubleshooting a property that does nothing
If /thread reports virtual=false, check these in order:
- Run
java -versionin the same environment that launches the application. A build on JDK 21 does not help if the container runs JRE 17. - Confirm the Boot version is 3.2 or later in your build file.
- Check that the property sits in the configuration file Boot actually loads. A profile-specific file such as
application-dev.propertiesapplies only when that profile is active. - Look for a custom
TaskExecutor,Executor, or Tomcat customizer in your own configuration. A bean you define replaces the auto-configured one. - Confirm the server is Tomcat or Jetty, not Undertow.
You can also check what Boot decided by starting with --debug and searching the conditions report for threading.
A safe starting configuration
For a first rollout, set the property in one place and keep the downstream limits explicit:
spring.threads.virtual.enabled=true
spring.datasource.hikari.maximum-pool-size=20
server.tomcat.max-connections=8192
server.tomcat.accept-count=100Writing the pool and connection limits down, even when they match the defaults, makes them visible in review. After the switch, they are the settings that actually bound your concurrency.
Exercise
You set spring.threads.virtual.enabled=true on a Spring Boot 3.3 application running on Java 21 with Tomcat. The /thread endpoint returns virtual=true. Your team lead then asks, "Will server.tomcat.threads.max=50 now keep us at 50 concurrent requests?" What is the answer, and which settings bound concurrency instead?
Show answer
No. With virtual threads, Tomcat starts a new virtual thread for each request, so threads.max no longer limits concurrent requests. Concurrency is bounded by server.tomcat.max-connections and accept-count at the connection level, and by downstream resources such as the Hikari pool size. To cap work against a limited resource, add an explicit limit such as a Semaphore around the call.
Exercise
The property is true and the application runs on Java 21, but /thread returns virtual=false. Name three possible causes.
Show answer
Any three of: the runtime JVM is older than 21 even though the build used 21; the Boot version is older than 3.2; the property is in a profile-specific file for a profile that is not active; the server is Undertow, which Boot 3.2 does not support for virtual threads; or a custom executor or Tomcat customizer bean replaced the auto-configured one.