Sitemap
Shweta Tyagi

Shweta Tyagi

👩‍💻 Mother, Wife, Coder | Passionate about Technology

Member-only story

Virtual Threads (Java 21)

2 min readJul 23, 2026

--

Press enter or click to view image in full size

For your entire career, one Java thread = one OS thread. That’s expensive — a few thousand threads and you’re out of memory. So we all learned to avoid threads: thread pools, async callbacks, reactive frameworks, CompletableFuture chains. Virtual threads throw that constraint out.

What changed

A virtual thread is a lightweight thread the JVM schedules onto a small pool of OS threads. When it blocks on I/O, the JVM unmounts it and runs another — so a blocked virtual thread costs almost nothing. You can have millions of them.

// This used to be insane. Now it's fine.
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 1_000_000; i++) {
executor.submit(() -> {
Thread.sleep(Duration.ofSeconds(1)); // blocks cheaply
return fetchFromDb();
});
}
}

Why it matters

The whole point: write plain, blocking, sequential code — and get async-level scalability. No callbacks, no reactive operators, no coloring functions async.

// Boring, readable, blocking — and it scales to huge concurrency now:
var user = userService.findById(id); // blocks
var orders = orderService.findByUser(user); // blocks
return new Dashboard(user, orders);

--

--

Shweta Tyagi
Shweta Tyagi

Written by Shweta Tyagi

👩‍💻 Mother, Wife, Coder | Passionate about Technology