Backend Development 7 min read

Using Java Virtual Threads in Spring Boot: Basics, Performance Experiments, and Optimization Tips

This article introduces Java 21 virtual threads, demonstrates their basic creation and delayed start, shows how to enable and configure them in Spring Boot, compares performance against traditional threads with benchmark results, and provides additional Java performance optimization techniques.

Architect
Architect
Architect
Using Java Virtual Threads in Spring Boot: Basics, Performance Experiments, and Optimization Tips

Virtual threads, introduced in Java 21, are lightweight JVM‑managed threads that simplify concurrency and enable massive parallelism.

Basic usage shows creating and starting a virtual thread:

Thread virtualThread = Thread.ofVirtual().start(() -> {
    System.out.println("虚拟线程正在运行");
});
System.out.println("主线程正在运行");

Virtual threads can also be created in a delayed‑start mode:

Thread virtualThread = Thread.ofVirtual()
    .name("虚拟线程")
    .unstarted(() -> System.out.println("虚拟线程运行中"));
virtualThread.start();
virtualThread.join(); // 等待虚拟线程完成

In Spring Boot, enable preview mode in pom.xml and configure Tomcat to use a virtual‑thread executor:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>21</source>
        <target>21</target>
        <compilerArgs>
            <arg>--enable-preview</arg>
        </compilerArgs>
    </configuration>
</plugin>

Enable performance monitoring in application.properties :

management.endpoints.web.exposure.include=health,info,metrics

Configure Tomcat’s protocol handler to use a virtual‑thread executor:

@Bean
public TomcatProtocolHandlerCustomizer
protocolHandlerVirtualThreadExecutorCustomizer() {
    return protocolHandler -> protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
}

Performance experiments compare 100,000 traditional threads with virtual threads. Traditional threads take about 18.6 seconds, while virtual threads complete in 3.7 seconds, a speed‑up of roughly 500%.

HTTP request benchmark (1,600 requests, 400 concurrent) shows traditional threads taking 9.66 seconds (165.65 req/s) versus virtual threads taking 7.91 seconds (202.22 req/s), demonstrating higher throughput and lower latency.

Additional Java performance tips include using parallel streams ( parallelStream() ) for CPU‑bound tasks, employing CompletableFuture for asynchronous I/O, optimizing database queries with caching (e.g., Redis), and using object pools such as Apache Commons Pool to reduce allocation overhead.

Conclusion: Virtual threads simplify thread management and dramatically improve performance in high‑concurrency Spring Boot applications, and when combined with other optimization techniques, they further enhance Java application efficiency.

JavaperformanceconcurrencySpring BootVirtual Threads
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.