Sitemap
Bolot Kasybekov

Bolot Kasybekov

Coding nerd!!! I love learning and discovering new programming languages. Big fan of Java and Javascript

Virtual Threads in Java

5 min readSep 23, 2025

--

Virtual threads (a key feature of Project Loom) are lightweight threads managed by the JVM rather than the OS. Unlike traditional platform threads (which wrap native OS threads with large stacks), virtual threads store their call stacks on the Java heap and are scheduled by the Java runtime. This means you can spawn thousands or even millions of virtual threads without exhausting OS resources. Project Loom introduced virtual threads (preview in Java 19/20, finalized in Java 21 via JEP 444) to make the “one thread per request” model scalable again. In effect, when a virtual thread blocks (e.g. waiting for I/O), the JVM parks it and frees its underlying OS thread to run other tasks. This lets code be written in simple blocking style (loops, try/catch, etc.) without manual async callbacks or heavy thread pools.

Figure 1. The Java documentation illustrates how a traditional thread pool (left) has a queue of tasks processed by a few OS threads, whereas a virtual-thread model (right) treats each task as its own thread (so “queued” tasks effectively become blocked threads). In practice, virtual threads let you think in terms of one task = one thread without overloading the OS. Platform threads are still thin wrappers around OS threads with large (MB-scale) stacks, whereas virtual threads have tiny initial stacks (on the order of hundreds of bytes) that grow on demand.

Press enter or click to view image in full size

Performance Comparison

In benchmarks, virtual threads shine under high concurrency. For example, one study replaced a traditional thread pool with virtual threads in a Spring Boot web service and saw a 3× throughput increase: median request latency dropped from ~15 s (platform threads) to ~5 s (virtual threads) when handling 1000 concurrent requests. Another benchmark suite found that under heavy load virtual threads consistently use far less CPU and memory: execution time and CPU load were substantially lower, and memory usage roughly halved compared to platform threads. This is because virtual threads avoid expensive OS context-switching and large per-thread stacks. In short, virtual threads provide scale, not raw speed — they don’t make individual tasks faster, but they let you process many more tasks in parallel. As the official docs note, “virtual threads are not faster threads; they exist to provide scale (higher throughput), not speed (lower latency)”

Get Bolot Kasybekov’s stories in your inbox

Join Medium for free to get updates from this writer.

Performance highlights:

  • Concurrency: Virtual threads can be created thousands of times faster (comparable to allocating a small object) and support vastly more concurrent tasks.
  • Throughput: Real-world tests (e.g. Spring+Tomcat) show much higher request throughput and lower tail latency when using virtual threads for I/O-heavy workloads.
  • CPU & Memory: Under load, virtual threads consume significantly less CPU (avoiding idle waits) and memory (small stacks) than an equivalent number of platform threads.

Real-World Use Cases

Virtual threads are ideal for I/O-bound, concurrent workloads where many tasks mostly wait on external resources. Common scenarios include:

  • Web servers & microservices: Each incoming HTTP request can be handled by a virtual thread, letting you use simple blocking code (database calls, REST clients, etc.) without tuning thread pools. Spring projects note that I/O-intensive network and database workloads see reduced memory footprint and higher concurrency with virtual threads. For example, configuring Tomcat or Spring’s async executors to use a newVirtualThreadPerTaskExecutor() lets your servlet threads run on virtual threads.
  • Database and messaging clients: Tasks that call JDBC, Kafka, RabbitMQ, etc. and spend time waiting can unleash more throughput if each call is on a virtual thread. With enough database connections, you can handle far more simultaneous queries than with a fixed OS-thread pool.
  • High-concurrency backends: Services like chat servers, multiplayer games, or any system with thousands of simultaneous sessions often benefited from reactive programming. Virtual threads let you revert to plain blocking code (loops and method calls) while achieving similar scalability. In one Spring benchmark, simply switching to virtual threads tripled throughput without any reactive boilerplatesixt.techspring.io.
  • Asynchronous APIs: Virtual threads make many async patterns obsolete. For example, the async Servlet API or callback-based frameworks were designed to save threads during waits; with virtual threads, you can just write synchronous code and trust Loom to yield when needed.
  • Batch or parallel tasks: Jobs that spin up many subtasks (e.g. simulations, map-reduce) can use a virtual-thread executor to simplify concurrency. Each task blocks or sleeps and yields, letting the CPU run other tasks.

In practice, adoption is growing. Frameworks and servers (Jetty, Tomcat, Spring, Helidon, etc.) are adding Loom support. For example, Spring’s team recommends adjusting your TaskExecutor or TomcatProtocolHandler to use virtual threads for higher throughput. Little’s Law still applies: more concurrent tasks need more connections, memory, and CPU to serve them. But by removing the overhead of OS threads, virtual threads often raise that ceiling.

Limitations and Caveats

Virtual threads are powerful, but not a silver bullet. Developers should be aware of several caveats:

  • CPU-bound workloads: If your code spends most time doing heavy computation (not waiting), virtual threads won’t improve throughput. In fact, Loom’s goal is concurrency for blocked threads. For CPU-intensive tasks, a fixed pool of platform threads (or parallel streams) is usually better.
  • Thread-local caching: Virtual threads still support ThreadLocal, but each virtual thread is typically used for one task and then discarded. Caching expensive objects in thread-locals (a common pattern) can backfire: every virtual thread will create its own instance, potentially using a lot of memory. It’s better to use immutable/shared objects (e.g. DateTimeFormatter) or Loom’s new scoped values.
  • Thread pinning: Certain operations “pin” a virtual thread to its OS thread, negating the usual benefits. In JDK 21–23, a virtual thread that blocks inside a synchronized block would pin its carrier thread. This was a known limitation (fixed in JDK 24). Pinning still occurs when calling native code via JNI, JNA or the Foreign Function API. In these cases, the OS thread cannot be reused until the block completes, which can degrade throughput. The advice is to avoid long-blocking calls inside synchronized or replace them with explicit locks, and to minimize JNI calls in hot virtual-thread paths.
  • Resource limits: Even with millions of threads available, you’re still limited by external resources. For example, a virtual-thread-heavy server is only as fast as its database or network can handle. If all tasks hit a limited connection pool, throughput will be bound by that pool (Little’s Law). Ensure your connection pools, file descriptors, and memory are sized for higher concurrency.
  • Tooling and maturity: Some debugging and profiling tools are still catching up to virtual threads. Older profilers or thread analyzers may not fully understand virtual threads. Also, the Loom APIs have evolved through preview releases. Be sure to use a recent JDK (Java 21+ or the latest update) for the best support.

--

--

Bolot Kasybekov
Bolot Kasybekov

Written by Bolot Kasybekov

Coding nerd!!! I love learning and discovering new programming languages. Big fan of Java and Javascript