Sub topic 6
What changes once the property is set
This chapter assumes you have a Spring Boot 3.2+ MVC application on Java 21 with Tomcat or Jetty. You should know ExecutorService and have a working idea of what a virtual thread is. The property spring.threads.virtual.enabled=true looks like one switch, but it changes several separate components. It also leaves several things alone. Knowing both lists lets you predict what you will see after the switch.
The one property
spring.threads.virtual.enabled=trueBoot reads this property through auto-configuration conditions. The conditions match only when the property is true and the JVM is Java 21 or newer. If the conditions do not match, Boot keeps its normal platform-thread setup and does not fail at startup. A wrong JDK therefore shows up as unchanged behavior, not as an error. Always confirm the effect at runtime, as shown at the end of this chapter.
What Boot switches over
The embedded web server
Each incoming request is handed to a thread. With the property set, that thread is a virtual thread.
- Tomcat: Boot replaces the protocol handler's executor with a thread-per-task executor that creates virtual threads. The threads are named with the prefix
tomcat-handler-. - Jetty: Boot configures the server's thread pool to run request handling on virtual threads.
The network acceptor and poller threads stay on platform threads. Only the code that runs your controller, filters, and interceptors moves to virtual threads.
Because Tomcat no longer uses a pooled executor for requests, server.tomcat.threads.max and server.tomcat.threads.min-spare stop limiting request concurrency. Other limits still apply: server.tomcat.max-connections (8192 by default for the NIO connector) and server.tomcat.accept-count. So Tomcat will accept far more simultaneous requests than before, and the limit moves elsewhere (see below).
The application task executor
Boot defines a bean named applicationTaskExecutor. Normally it is a ThreadPoolTaskExecutor sized by spring.task.execution.pool.*. With the property set, it becomes a SimpleAsyncTaskExecutor that creates a new virtual thread for each task, with the thread name prefix task-.
Several things use this bean by default:
- Methods annotated with
@Async, when@EnableAsyncis present and you have not named another executor. - Asynchronous request processing in Spring MVC, such as controllers that return
CallableorDeferredResultbacked by the executor. - Other framework components that look up the application task executor.
Since the new executor creates a thread per task, the spring.task.execution.pool.* properties (core size, max size, queue capacity) no longer have any effect on it. There is no queue, so there is also no queue-based back pressure.
The task scheduler
Boot's scheduler bean for @Scheduled methods becomes a SimpleAsyncTaskScheduler that runs each triggered task on a virtual thread. The thread names start with scheduling-. Overlapping runs of a slow task behave as before, because the trigger rules decide when a task fires. If you rely on a small scheduler pool to serialize different scheduled jobs, check that assumption, because jobs may now run in parallel.
Some messaging integrations (for example Kafka and RabbitMQ listener containers) also pick up virtual threads through Boot in recent versions. The exact list depends on your Boot version, so check the release notes for the version you run instead of assuming.
What stays the same
| Component | Behavior after the property is set |
|---|---|
| HikariCP pool | Still limited by `spring.datasource.hikari.maximum-pool-size`, which is 10 by default |
| Your own `Executor` or `ThreadPoolTaskExecutor` beans | Unchanged, still platform threads unless you change them |
| `CompletableFuture.supplyAsync(...)` without an executor | Runs on `ForkJoinPool.commonPool()`, a platform thread pool |
| Parallel streams | Use the common pool, unchanged |
| Code under `synchronized` that blocks | Still pins the carrier thread on Java 21 (see the pinning chapter) |
| Reactive (WebFlux) stack | Not affected by this property |
The connection pool row matters most. With 200 platform request threads and a pool of 10 connections, at most 200 requests could be waiting for a connection. With virtual threads there may be thousands, all queued on the same 10 connections. Each waiter blocks until spring.datasource.hikari.connection-timeout (30 seconds by default) and then fails. The switch can move the bottleneck from the thread pool to the connection pool without improving throughput. The chapter on limiting concurrency covers how to handle this.
Your own Executor beans disable the default
Boot creates applicationTaskExecutor only when no other Executor bean exists in the context. If you define any Executor bean, Boot's default backs off. Your @Async calls and async MVC requests then use your bean, even though the property is set.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
class AsyncConfig {
@Bean
SimpleAsyncTaskExecutor reportExecutor() {
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("report-");
executor.setVirtualThreads(true);
executor.setConcurrencyLimit(8);
return executor;
}
}This bean runs each task on a virtual thread, and setConcurrencyLimit(8) makes callers wait once eight tasks are running. A limit like this is how you restore the back pressure that the unbounded default lacks. If you keep an old ThreadPoolTaskExecutor bean, it continues to use platform threads, and the property will not convert it.
Verifying what actually runs where
Do not assume. Add a small endpoint that reports the current thread, and a second path through @Async.
import java.util.concurrent.CompletableFuture;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
class AsyncProbe {
@Async
CompletableFuture<String> describe() {
Thread current = Thread.currentThread();
return CompletableFuture.completedFuture(
"virtual=" + current.isVirtual() + ", name=" + current.getName());
}
}import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ProbeController {
private final AsyncProbe probe;
ProbeController(AsyncProbe probe) {
this.probe = probe;
}
@GetMapping("/probe")
String probe() throws Exception {
Thread request = Thread.currentThread();
String async = probe.describe().get();
return "request: virtual=" + request.isVirtual()
+ ", name=" + request.getName() + " | async: " + async;
}
}This probe needs @EnableAsync on a configuration class, as in AsyncConfig above. If you use that class unchanged, the async part reports the report- prefix. Remove the reportExecutor bean to see Boot's default executor with the task- prefix.
With the property on and the default setup, a call to /probe returns something like request: virtual=true, name=tomcat-handler-3 | async: virtual=true, name=task-1. If you see virtual=false and a name such as http-nio-8080-exec-1, one of these is the cause:
- The JVM is older than 21.
- The property is not being loaded (wrong profile, wrong file, or a typo).
- A custom bean overrides the default component.
Note that a virtual thread's getName() is empty unless someone assigns a name. Boot assigns the prefixes above, which is why these names are useful for log filtering.
Common errors
- Tuning the old pool settings. Changing
server.tomcat.threads.maxafter enabling virtual threads has no effect on request concurrency. Limit concurrency at the scarce resource instead. - Assuming all async work moved.
CompletableFuture.supplyAsyncwithout an executor argument still uses the common pool. Pass a virtual-thread executor when you want blocking work off it. - Forgetting the connection pool. A flood of virtual threads waiting on 10 connections produces timeouts, not speed.
- Expecting a queue. The default
applicationTaskExecutornever queues. Every submitted task starts immediately unless you set a concurrency limit. - Trusting the property alone. An unrelated
Executorbean in a dependency's configuration can silently replace the default. Check with the probe.
Exercise
After you set spring.threads.virtual.enabled=true, you raise server.tomcat.threads.max from 200 to 2000 to "give virtual threads more room". Why does this change nothing, and which two settings are more likely to limit your application's concurrency now?
Show answer
With the property set, Tomcat creates a new virtual thread per request instead of using a pooled executor, so server.tomcat.threads.max is not used to limit request handling. Concurrency is now limited by other resources. Two likely ones are the connection pool size (spring.datasource.hikari.maximum-pool-size, 10 by default), which makes extra requests wait for a connection, and the capacity of downstream services. Tomcat's own max-connections and accept-count settings also still apply.
Exercise
Your application defines a ThreadPoolTaskExecutor bean named mailExecutor. You enable virtual threads and call an @Async method without naming an executor. What thread type runs it, and why?
Show answer
It runs on a platform thread from mailExecutor. Boot creates applicationTaskExecutor only when no other Executor bean exists. Because mailExecutor is an Executor bean, the default backs off, so @Async uses mailExecutor. The property does not convert it to virtual threads. To change this, define the bean as a SimpleAsyncTaskExecutor with setVirtualThreads(true), or remove the custom bean.