Virtual threads are lightweight; you can run millions without exhausting the OS.
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
var f1 = executor.submit(() -> "task-1");
var f2 = executor.submit(() -> "task-2");
System.out.println(f1.get() + " " + f2.get());
}
task-1 task-2
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26