Careeroza
Virtual Threads vs Platform Threads
Comparison
| Platform thread | Virtual thread | |
|---|---|---|
| Managed by | OS | JVM |
| Memory | ~1 MB stack reserved | small, grows on the heap |
| Creation cost | expensive (system call) | cheap (object allocation) |
| How many | thousands | millions |
| Pool them? | yes (thread pools) | no — create one per task |
| Best for | CPU-bound work | I/O-bound, blocking code |
| Daemon | configurable | always daemon |
| ThreadLocal | fine | works, but millions of copies can use a lot of memory |
Virtual Threads vs Reactive
Before Loom, high-concurrency Java used reactive libraries (WebFlux, RxJava): non-blocking but hard to read and debug. Virtual threads give similar scalability with plain, blocking, step-by-step code and normal stack traces.
// Reactive (WebFlux)
return userRepo.findById(id)
.flatMap(user -> orderClient.ordersFor(user.id())
.map(orders -> new Profile(user, orders)));
// Virtual threads: same scalability, ordinary code
User user = userRepo.findById(id).orElseThrow();
List<Order> orders = orderClient.ordersFor(user.id());
return new Profile(user, orders);