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 and have already added spring.threads.virtual.enabled=true to application.properties. It covers what that one property changes and what it leaves alone.
The property swaps executors
The property does not rewrite your code. Boot reads it during auto-configuration and replaces the executors it creates with ones that start a new virtual thread per task. Your controllers, services, and repositories stay the same. They run on a different kind of thread.
What moves over
| Component | Before | After |
|---|---|---|
| Tomcat or Jetty request handling | Worker pool of platform threads | One virtual thread per request |
| `applicationTaskExecutor` (used by `@Async` and MVC async support) | Pooled platform threads | `SimpleAsyncTaskExecutor` that starts a virtual thread per task |
| `taskScheduler` (used by `@Scheduled`) | `ThreadPoolTaskScheduler` | `SimpleAsyncTaskScheduler` that runs each job on a virtual thread |
Request handling is the part that matters most. Each incoming request now gets its own virtual thread. When your code blocks on JDBC or an HTTP call, the virtual thread unmounts from its carrier thread, so the carrier can run other requests.
What does not change
- Pool-size properties on the task executor.
spring.task.execution.pool.*settings configure a pool. Once the executor starts one thread per task, there is no pool for them to configure. - The connection pool. Hikari still holds
maximum-pool-sizeconnections. With thousands of virtual threads, most requests wait for a connection. That is covered in the chapter on protecting downstream services. - Executors you build yourself. A
Executors.newFixedThreadPool(10)bean stays a fixed pool of platform threads. Boot only changes what it auto-configures. - Tomcat's
max-threads. It no longer limits request concurrency, because requests no longer use the worker pool.
Check that it worked
Add an endpoint that reports the thread type. This is the first verification step in any rollout.
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableAsync
@Service
class AsyncProbe {
@Async
public void log() {
System.out.println("async virtual=" + Thread.currentThread().isVirtual());
}
}
@RestController
class ThreadInfoController {
private final AsyncProbe probe;
ThreadInfoController(AsyncProbe probe) {
this.probe = probe;
}
@GetMapping("/thread")
String thread() {
probe.log();
Thread current = Thread.currentThread();
return "virtual=" + current.isVirtual() + ", thread=" + current;
}
}Call /thread. The response should say virtual=true, and the console should print async virtual=true. Place @EnableAsync on a configuration class or the main application class, as you normally would. It sits on the service here only to keep the example in one file.
Common mistakes
- Assuming
@Asyncmethods are still bounded. Without a pool, a burst of calls starts a burst of threads. If those threads call a limited service, add aSemaphore. - Tuning
server.tomcat.threads.maxand expecting an effect. It no longer caps concurrency. - Checking only the web layer. Test
@Asyncand@Scheduledcode paths separately, since each uses its own executor.
Exercise
After enabling the property, you still have a bean Executors.newFixedThreadPool(8) that your report-generation code uses. Do those tasks run on virtual threads? What would you change to make them do so?
Show answer
No. Boot only replaces the executors it auto-configures, so your fixed pool keeps its 8 platform threads. To use virtual threads, create the executor with Executors.newVirtualThreadPerTaskExecutor() instead, and add a Semaphore if the tasks call a resource that needs a concurrency limit.