Sub topic 5
Turning on virtual threads in Spring Boot
This chapter assumes you know Java 21 basics, including ExecutorService, and that you have a working Spring Boot 3.2 or later MVC application running on Tomcat or Jetty. You should also be comfortable editing application.properties or application.yml.
What you need first
Virtual threads need two things from your build:
- Java 21 or newer as the runtime. Virtual threads were finalized in Java 21 (JEP 444).
- Spring Boot 3.2 or newer. Earlier versions do not have the property described below.
If either is missing, the property is ignored or the application fails to start. Check the Java version first, because a build that compiles on 21 can still be launched on an older JVM in a container or CI image.
The single property
One line in application.properties switches Boot over:
spring.threads.virtual.enabled=trueThe YAML form is equivalent:
spring:
threads:
virtual:
enabled: trueNo code changes are needed. Boot reads the property while auto-configuring the embedded web server and the application task executor. Both then create a new virtual thread for each unit of work, instead of taking a thread from a fixed pool.
The next chapter lists exactly which components change. For now, the important point is that your existing controllers, services, and JDBC or HTTP client code stay the same. They still block, but a blocked virtual thread releases its carrier thread so the carrier can run other work.
Checking that it worked
Do not assume the property took effect. Add a small endpoint that reports the type of 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 the endpoint:
curl http://localhost:8080/threadWith the property on, the response starts with virtual=true. The thread description is typically VirtualThread[#...]/runnable@ForkJoinPool-1-worker-1. The part after the @ names the carrier thread that is running it at that moment.
With the property off, you see virtual=false and a name such as http-nio-8080-exec-1. That name comes from Tomcat's request pool.
The flow below shows what the property changes.
Common errors
- The endpoint reports
virtual=false. Confirm that the JVM is Java 21 or newer and that the property is in the file Boot actually loads. A profile-specific file or an environment variable can override it. The environment variable form isSPRING_THREADS_VIRTUAL_ENABLED=true. - Wrong Boot version. On Boot 3.1 or older the property does nothing. Upgrade to 3.2 or later.
- Old pool settings still in the configuration. Settings such as
server.tomcat.threads.maxare not removed by the property. Their effect on virtual threads is covered in the next chapter, so do not delete or tune them yet.
Summary of the steps
- Confirm the runtime is Java 21 or newer and Boot is 3.2 or newer.
- Set
spring.threads.virtual.enabled=true. - Restart the application.
- Call a diagnostic endpoint and confirm
virtual=true.
Exercise
You set spring.threads.virtual.enabled=true on a Boot 3.2 application, but /thread still returns virtual=false and the thread name begins with http-nio. Name two things to check.
Show answer
Check that the application actually runs on Java 21 or newer (the build tool may target 21 while the launch environment uses an older JVM). Check that the property is in the configuration Boot loads, and that no profile-specific file or environment variable overrides it with false.