How Java Virtual Threads Supercharge SpringBoot: 200× Faster Than Classic Threads

This article explains Java virtual threads, compares them with traditional OS threads, shows how to enable them in SpringBoot 3, and presents performance benchmarks demonstrating up to 200‑fold speed improvements for I/O‑bound workloads.

macrozheng
macrozheng
macrozheng
How Java Virtual Threads Supercharge SpringBoot: 200× Faster Than Classic Threads

What Is a Virtual Thread

Virtual threads are a feature added in Java 19 that works like Goroutine in Go, providing lightweight, user‑level threads managed by the JVM instead of the operating system.

Difference Between Virtual and Platform Threads

Virtual threads are "fake" threads that are not scheduled directly by the OS; a single platform thread can multiplex thousands of virtual threads, consuming far less memory and allowing creation of millions of them when resources permit.

Using Virtual Threads in SpringBoot

By configuring SpringBoot 3 to use virtual threads, the default async thread pool and Tomcat HTTP thread pool can be replaced, yielding dramatic performance gains.

@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 {
    @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");
}

Results:

Platform thread time: 678 seconds (over 10 minutes)

Virtual thread time:

3.9 seconds

The performance gap is roughly 200×.

HTTP Request Performance Comparison

A simple GET endpoint that sleeps 50 ms is tested with JMeter using 500 concurrent threads and 10 000 requests.

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

Platform threads: minimum 50 ms, but median and 90/95/99 percentiles exceed 150 ms due to thread pool exhaustion.

Virtual threads: maximum response time stays below 100 ms, showing much lower wait times and better resource utilization.

Conclusion

Virtual threads provide a clear performance advantage for I/O‑bound workloads, such as typical web applications that spend most of their time waiting on database, cache, or network I/O. For CPU‑bound tasks the benefit is smaller, but for the majority of Java web services the gains are substantial.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformanceconcurrencySpringBootVirtual Threads
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.