Java Concurrency and Virtual Threads
Thread pools, CompletableFuture, reactive programming and virtual threads; when Loom helps, pinning, structured concurrency and production guidance.
Concurrency models in Java
| Model | How | Strength | Weakness |
|---|---|---|---|
| Platform threads + pools | One OS thread per task | Simple, debuggable | Threads are expensive; limits concurrency to thousands |
CompletableFuture |
Async composition | Non-blocking composition | Callback-style code, harder debugging |
| Reactive (Project Reactor, WebFlux) | Event loops, backpressure | Very high concurrency with few threads, streaming | Steep learning curve, viral async APIs, hard stack traces |
| Virtual threads (Java 21+) | Lightweight threads scheduled by the JVM onto carrier threads | Blocking-style code with massive concurrency | Not faster for CPU-bound work; resource limits still matter |
How virtual threads work
A virtual thread is a Java object; when it blocks on I/O (socket read, Thread.sleep, lock in 24+), the JVM unmounts it from its carrier (platform) thread and mounts another. Millions of virtual threads can exist because each uses only a small heap-allocated stack.
flowchart LR VT1[Virtual thread 1<br/>waiting on DB] -. parked .-> H[(Heap)] VT2[Virtual thread 2<br/>running] --> C1[Carrier thread 1] VT3[Virtual thread 3<br/>running] --> C2[Carrier thread 2] VT4[Virtual thread 4<br/>waiting on HTTP] -. parked .-> H C1 & C2 --> CPU[CPU cores]
Using virtual threads
// Plain Java
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<Quote>> quotes = suppliers.stream()
.map(s -> executor.submit(() -> s.quote(request))) // each call blocks a cheap virtual thread
.toList();
for (var f : quotes) results.add(f.get());
}# Spring Boot 3.2+: run request handling and @Async on virtual threads
spring.threads.virtual.enabled=truePitfalls
- Pinning: before Java 24, blocking inside
synchronizedpinned the carrier thread. JEP 491 (Java 24) removed this forsynchronized; native calls and some JNI paths can still pin. Diagnose with JFR eventjdk.VirtualThreadPinned. - Resource limits: virtual threads remove the thread limit, not the database connection limit. Without a bounded pool or semaphore, 100K concurrent requests can overwhelm a database. Use bulkheads.
- ThreadLocal-heavy code: millions of virtual threads × large ThreadLocals = memory. Prefer scoped values.
- CPU-bound work: no benefit; use a sized platform-thread pool or parallel streams.
- Do not pool virtual threads: create one per task.
Structured concurrency (preview through Java 25)
Treats a group of concurrent subtasks as one unit: if one fails, siblings are cancelled; the parent waits for all.
// Preview API: requires --enable-preview; API shape changed across previews
try (var scope = StructuredTaskScope.open()) {
Subtask<User> user = scope.fork(() -> userService.find(id));
Subtask<List<Order>> orders = scope.fork(() -> orderService.recent(id));
scope.join(); // throws if any subtask failed, cancelling the others
return new Profile(user.get(), orders.get());
}[!VERSION] Structured concurrency was redesigned in Java 25's preview (static
open()factory and joiners). Expect further changes before it is final.
Choosing a model
| Situation | Recommendation |
|---|---|
| New I/O-heavy REST services (DB, HTTP calls) | Virtual threads with Spring MVC |
| Streaming, backpressure, WebSockets at huge scale | Reactive (WebFlux) remains a strong option |
| CPU-bound processing | Fixed platform-thread pools sized to cores |
| Existing reactive codebase that works well | Keep it; migrate only with clear benefit |
Interview questions and answers
Do virtual threads make my application faster?
They increase throughput for I/O-bound workloads by allowing far more concurrent blocking operations. Latency per request does not improve, and CPU-bound workloads see no gain.
What replaced the need for reactive programming?
For many request/response services, virtual threads give similar scalability with simple blocking code. Reactive still fits streaming pipelines and backpressure-heavy systems.
How do you protect a database when using virtual threads?
Limit concurrency explicitly: a bounded connection pool (HikariCP) with sensible timeouts, semaphores around expensive calls, and rate limiting at the edge.