Lesson 4 of 9 · 28m
Virtual Threads: The Mental Model That Prevents Bad Advice
Understand what virtual threads cheapen, what they do not cheapen, and how blocking code, synchronization, and thread locals behave in production.
Virtual threads are the largest practical concurrency shift in Java since the fork/join era, and the first thing to unlearn is the phrase 'lightweight thread means unlimited threads.' Virtual threads make blocking cheap enough that ordinary synchronous code can scale. They do not make memory free, downstream capacity infinite, or bad locking suddenly virtuous.
What Loom actually changed
A platform thread is a thin wrapper around an operating-system thread. A virtual thread is scheduled by the JVM onto a small pool of carrier threads. When it blocks on most JDK IO operations, it can unmount and let the carrier run another virtual thread.
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (Order order : orders) {
executor.submit(() -> fulfill(order));
}
}Notice the style: one executor, many independent tasks, try-with-resources waiting for completion. You did not write reactive callbacks, chain futures, or invent a bespoke thread-pool taxonomy. That is the point.
Blocking is no longer the enemy — resource exhaustion is
Old Java advice said never block limited pools because a few blocked threads could starve the entire service. Virtual threads invert the cost model, but the database still has finite connections, the downstream API still has rate limits, and heap memory still exists. Often the connection pool itself is the correct limiter. The discipline did not disappear; it moved from 'avoid blocking' to 'bound scarce resources explicitly.'
CPU-bound work is not virtual-thread work
Virtual threads help when tasks spend time waiting. Compression, image processing, serialization-heavy transformations, password hashing, and numeric computation do not become parallel just because the tasks are virtual. Use platform threads or dedicated executors for CPU-bound sections, then hand results back to virtual-thread workflows. If a workload burns CPU while holding the carrier, adding more virtual threads mostly adds queue depth.
Pinning and synchronization
On Java 21, a virtual thread can become pinned to its carrier when blocking while inside synchronized code. Short synchronized sections are usually harmless; blocking network calls inside broad synchronized sections are not.
synchronized (cache) {
return cache.computeIfAbsent(key, this::loadOverNetwork); // risky on Java 21
}Move IO outside the lock or use ReentrantLock with a clear critical section. Later JDK releases improve this behavior, but the design rule survives: locks guard memory consistency, not network latency.
Thread locals deserve suspicion
Virtual threads are numerous and short-lived. Large mutable ThreadLocal caches are memory leakage waiting for a traffic spike, and inherited thread-local assumptions from platform-thread pools become unreliable.
Use request-scoped objects passed explicitly, or investigate scoped values for contextual propagation in JDK versions where they are supported. If you need a per-thread object pool because allocation is expensive, measure first; object pooling in modern Java is often a superstition carried forward from a different JVM.
Field notes
newVirtualThreadPerTaskExecutormeans a new thread per task, not a magical unbounded safe system.- Bound downstream concurrency with connection pools, permits, queues, and backpressure.
- Never cache a virtual thread as if it were a managed worker identity.
- Interrupt handling still matters; virtual threads are not immune to cancellation semantics.
- Watch heap, socket count, database connections, and latency percentiles — not just thread count.
Was this lesson helpful?