Project Loom Virtual Threads (JEP 444) decouple Java threads from OS kernel threads, allowing applications to launch 1,000,000+ concurrent threads. Learn how to diagnose Carrier Thread Pinning and replace ThreadLocal with Scoped Values (JEP 446).
1. Executing Tasks with Virtual Thread Executors
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 10_000).forEach(i -> {
executor.submit(() -> {
// High-throughput blocking I/O operation
Thread.sleep(Duration.ofSeconds(1));
return i;
});
});
} // Auto-joins all virtual threads upon scope exit