Using Java Virtual Threads in Spring Boot: Configuration and Performance Comparison

The article explains Java's virtual threads introduced in Java 19, compares them with traditional threads, and demonstrates how to configure Spring Boot to use virtual threads, including code samples and performance benchmarks that show dramatic latency reductions in both async services and HTTP requests.

IT Xianyu
IT Xianyu
IT Xianyu
Using Java Virtual Threads in Spring Boot: Configuration and Performance Comparison

What are Virtual Threads

Virtual threads are a feature added in Java 19, similar to Go's goroutines, providing lightweight thread-like constructs that are scheduled by the JVM rather than the operating system.

Difference between Virtual and Regular Threads

Virtual threads are “fake” threads that do not map directly to OS threads; a single OS thread can schedule thousands of virtual threads, resulting in much lower memory and CPU overhead, allowing creation of millions of virtual threads.

Actually, if you have used Akka you will notice the similarity; the difference is that Akka is handled by the application while virtual threads are handled by the JVM, making usage simpler and more convenient.

Using Virtual Threads in Spring Boot

The article shows how to replace Spring Boot's default asynchronous thread pool and Tomcat HTTP thread pool with virtual threads, providing configuration code.

Configuration

Java version: java-20.0.2-oracle; Spring Boot version: 3.1.2. Add the following configuration:

/**
 * Configuration used for testing; spring.virtual-thread=true enables virtual threads, false keeps regular threads.
 */
@Configuration
@ConditionalOnProperty(prefix = "spring", name = "virtual-thread", havingValue = "true")
public class ThreadConfig {

    @Bean
    public AsyncTaskExecutor applicationTaskExecutor() {
        return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
    }

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

@Async Performance Comparison

An asynchronous service that sleeps for 50 ms (simulating I/O) is invoked 100,000 times.

@Service
public class AsyncService {

    /**
     * @param countDownLatch used for testing
     */
    @Async
    public void doSomething(CountDownLatch countDownLatch) throws InterruptedException {
        Thread.sleep(50);
        countDownLatch.countDown();
    }
}
@Test
public void testAsync() throws InterruptedException {
    long start = System.currentTimeMillis();
    int n = 100000;
    CountDownLatch countDownLatch = new CountDownLatch(n);
    for (int i = 0; i < n; i++) {
        asyncService.doSomething(countDownLatch);
    }
    countDownLatch.await();
    long end = System.currentTimeMillis();
    System.out.println("耗时:" + (end - start) + "ms");
}

Regular threads take about 678 seconds (over 10 minutes); virtual threads finish in approximately 3.9 seconds, a roughly 200‑fold speedup.

HTTP Request Performance Comparison

A simple GET endpoint that sleeps 50 ms is benchmarked with 500 concurrent threads for 10,000 requests using JMeter.

@RequestMapping("/get")
public Object get() throws Exception {
    Thread.sleep(50);
    return "ok";
}

With regular threads, median latency exceeds 150 ms despite the 50 ms sleep, due to thread pool exhaustion; virtual threads keep maximum latency below 100 ms, dramatically reducing wait time.

Conclusion

Virtual threads provide clear performance advantages for I/O‑bound workloads such as typical web applications, while benefits for CPU‑bound tasks are limited. Upgrading from older Java versions (e.g., Java 8) is advisable to leverage this capability.

JavaSpring BootVirtual Threads
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

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.