Introduction: Think of Virtual Threads as Tasks, Not Pooled Resources
With traditional OS platform threads, threads are expensive operating-system resources. That scarcity trained Java developers to pool threads (Executors.newFixedThreadPool()) and share them across thousands of tasks.
With Java Virtual Threads (finalized in Java 21), that mental model flips completely: a virtual thread is as cheap as a task object itself. Instead of pooling virtual threads, you spawn a brand-new virtual thread for every single task, let it block freely on I/O, and let the Garbage Collector clean it up when done.
In this guide, we cover:
- Running fire-and-forget or scoped tasks with
Executors.newVirtualThreadPerTaskExecutor() - Returning values from virtual threads using
Future<T> - Running parallel batches with
ExecutorService.invokeAll()vs.StructuredTaskScope - Scheduling delayed or periodic virtual threads with
ScheduledExecutorService
1. Using Executors.newVirtualThreadPerTaskExecutor()
Executors.newVirtualThreadPerTaskExecutor() creates an ExecutorService that spawns a fresh, unpooled virtual thread for every submitted Runnable or Callable.
Because ExecutorService extends AutoCloseable in Java 19+, you can use it inside a try-with-resources statement. The try block automatically waits for all submitted virtual threads to finish before continuing:
| |
Why You Should Never Pool Virtual Threads: Passing a virtual
ThreadFactoryintoExecutors.newFixedThreadPool(10, factory)ornewSingleThreadExecutor(factory)defeats the entire purpose of virtual threads. As Ron Pressler and Alan Bateman emphasize, if you need to rate-limit concurrent requests to a database or third-party API, use aSemaphoreinstead of a thread pool.
2. Returning Values from Virtual Threads with Future<T>
Submitting a Callable<T> to newVirtualThreadPerTaskExecutor() returns a Future<T>. Calling future.get() inside a virtual thread unmounts the waiting virtual thread without blocking the underlying OS carrier thread:
| |
3. Running Parallel Tasks with invokeAll() vs. StructuredTaskScope
When you want to fan out multiple I/O tasks in parallel and collect all their results, you have two primary options in modern Java:
Option A: ExecutorService.invokeAll() with Virtual Threads
invokeAll() submits a collection of Callable tasks, runs each on its own virtual thread concurrently, and blocks until all futures complete:
| |
Option B: Fail-Fast Parallel Execution with StructuredTaskScope
With ExecutorService.invokeAll(), if the first task throws an exception after 10ms, invokeAll() still waits for the slowest task to finish before returning. If you want fail-fast cancellation, use Structured Concurrency (StructuredTaskScope.ShutdownOnFailure):
| |
4. Using ScheduledExecutorService with Virtual Threads
What if you want to schedule delayed or periodic tasks on virtual threads? You can pass a virtual ThreadFactory into Executors.newScheduledThreadPool(0, factory) with corePoolSize = 0 so idle threads aren't pooled:
| |
Conclusion & Further Reading
Thinking of virtual threads as lightweight, disposable tasks—rather than scarce OS resources that must be pooled—unlocks clean, high-throughput concurrent code with standard ExecutorService and Future APIs.
Explore more concurrency deep dives:
- Java Virtual Threads Explained: Architecture, Creation & Performance
- The Hidden Cost of Java Virtual Threads: Deep Stacks, GC Pressure & Cache Locality
- Structured Concurrency in Java:
StructuredTaskScope,ShutdownOnFailure& Deadlines - Java
ExecutorServiceComplete Tutorial - Running Multiple Threads and Getting the First Result (
invokeAnyvs.ShutdownOnSuccess)
Adopt Virtual Threads & Structured Concurrency Without Production Surprises
Virtual threads look simple on paper—until carrier thread pinning, unmounted stack heap pressure, or legacy ThreadLocals hit production. Bring me in for a tailored 1-day Java Virtual Threads, Structured Concurrency & Scoped Values Workshop built around your team's actual architecture and frameworks.
Want More Deep-Dive JVM Benchmarks & Project Panama Research?
Follow my weekly engineering posts on LinkedIn, explore the full JMH benchmark suites and pure-Java Linux io_uring / FUSE projects on GitHub, or download my conference talk slides: