DEV Community

Idev.d
Idev.d

Posted on Originally published at idev.my

Java 21 Virtual Threads: Is the Thread Pool Era Over?

Java 21 officially introduces virtual threads (Project Loom), making high-concurrency programming simpler than ever.

What Are Virtual Threads

Virtual threads are lightweight threads managed by the JVM, not the OS. You can create millions of them, while platform threads are limited to thousands.

Traditional thread pools have a ceiling: each platform thread uses ~1MB of stack memory. 200 threads = 200MB, and IO-blocking operations (DB queries, HTTP calls) waste thread capacity.

Virtual threads automatically yield the underlying platform thread when hitting IO blocks, letting other virtual threads continue. A few platform threads can support massive concurrent requests.

Benchmark Results

We tested a typical Spring Boot + MySQL app (1000 concurrent requests, each with one DB query):

Metric Thread Pool (200) Virtual Threads
Throughput (req/s) 850 2,400
Avg Response Time 235ms 42ms
P99 Response Time 1,200ms 180ms
Memory Usage 450MB 280MB

Enabling in Spring Boot 3.2+

spring:
  threads:
    virtual:
      enabled: true
Enter fullscreen mode Exit fullscreen mode

One line. That's it. Spring Boot replaces Tomcat's thread pool with virtual threads.

Important Caveats

  • Not for CPU-bound work: Virtual threads can't yield during computation — they only benefit IO-bound tasks
  • Avoid synchronized blocks: Virtual threads don't unmount from platform threads inside synchronized. Use ReentrantLock instead
  • ThreadLocal caution: May cause memory issues. Consider ScopedValue as an alternative
  • Connection pools: Your DB connection pool might become the bottleneck instead — monitor HikariCP max connections

Small team, big output. iDev builds web apps, AI solutions and custom systems with startup speed and enterprise quality. Based in Malaysia, serving Southeast Asia. Free consultation.

Top comments (0)