Unlock Java 21 Performance with Spring Boot 3.2, Virtual Threads & GraalVM Native Images
This article demonstrates how Spring Boot 3.2 fully supports Java 21, Project Loom virtual threads, and GraalVM native images, guiding you through installation, configuration, sample code, and performance benefits of running a Spring application on virtual threads and native compilation.
Spring Boot 3.2 Release
Spring Boot 3.2 has been officially released and brings out‑of‑the‑box support for Java 21, Project Loom virtual threads, and GraalVM native images.
Supported Features
Java 21
Virtual Threads
Native Image (GraalVM)
Java 21
Spring Boot 3.2 fully supports the Java 21 release, which adds thousands of performance, stability, and security improvements.
Project Loom – Virtual Threads
Virtual threads are a key feature of Project Loom, providing lightweight concurrency. The official JEP 444 explains the implementation.
GraalVM and Native Image
GraalVM is a high‑performance JDK that can compile Java code ahead‑of‑time into a native executable, offering faster startup and lower runtime memory usage compared with the JVM.
Try It Out
Install Java 21.0.1‑graal via SDKMAN:
sdk install java 21.0.1-graal
sdk default java 21.0.1-graalAlternatively, download GraalVM manually from https://www.graalvm.org/downloads/ .
Create a new Spring Boot project (3.2.0) with Java 21, Gradle‑Groovy, Spring Web, and GraalVM native support.
Enable virtual threads by adding the property: spring.threads.virtual.enabled: true This makes Tomcat handle HTTP requests on virtual threads, runs @Async methods and @Scheduled tasks on virtual threads, and allows other integrations (e.g., RabbitMQ, Kafka) to benefit where supported.
Code Samples
Simple controller:
@RestController
@RequestMapping("/test")
public class TestController {
private static final Logger log = LoggerFactory.getLogger(TestController.class);
@GetMapping
public void test() {
log.info("Rest controller method has been called {}", Thread.currentThread());
}
}Asynchronous task:
@Component
public class AsyncTaskExecutorService {
private static final Logger log = LoggerFactory.getLogger(AsyncTaskExecutorService.class);
@Async
public void run() {
log.info("Async task method has been called {}", Thread.currentThread());
}
}Scheduled task (every 15 seconds):
@Component
public class SchedulerService {
private static final Logger log = LoggerFactory.getLogger(SchedulerService.class);
@Scheduled(fixedDelayString = "15000")
public void run() {
log.info("Scheduled method has been called {}", Thread.currentThread());
}
}Run the application: ./gradlew bootRun Call the endpoint: curl -X GET 'localhost:8085/test' Sample log output shows each method executing on a virtual thread from the common ForkJoinPool, confirming that Tomcat and Spring components are using virtual threads.
According to JEP 444, the virtual‑thread scheduler is a work‑stealing ForkJoinPool that runs in FIFO order with parallelism equal to the number of platform threads available.
Native Image Build
Build a GraalVM native image:
./gradlew nativeCompile
./build/native/nativeCompile/appThe native executable starts significantly faster, confirming the claimed startup‑time and memory‑usage advantages over the JVM.
The full source code is available at https://github.com/egor-ponomarev/spring-boot3.2-with-graalvm-virtual-threads-example .
Conclusion
Spring Boot 3.2 enables developers to combine Java 21, virtual threads, and GraalVM native images, achieving Go‑like performance while retaining the rich JVM ecosystem. However, not all libraries are yet compatible with virtual threads, so careful testing is required.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
