Java 21: Virtual threads for massive concurrency

Virtual threads are lightweight; you can run millions without exhausting the OS.

Code
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    var f1 = executor.submit(() -> "task-1");
    var f2 = executor.submit(() -> "task-2");
    System.out.println(f1.get() + " " + f2.get());
}
Output
task-1 task-2
Advertisement

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