Executors & Virtual Threads in Java

From the Java cheat sheet · Concurrency · verified Jul 2026

Executors & Virtual Threads

Thread pools, futures, and lightweight virtual threads (21+).

java
// Quick Reference
try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
    ex.submit(() -> handle());
}
Thread.startVirtualThread(() -> work());   // Java 21+
💡 Prefer an ExecutorService over creating raw Threads - it manages pooling and lifecycle.
⚡ Virtual threads (Java 21+) are lightweight - block freely on I/O without exhausting the pool.
📌 Future.get() blocks until the result is ready (or throws if the task failed).
🟢 ExecutorService is AutoCloseable since Java 19 - try-with-resources awaits and shuts down.
executorsvirtual-threads

More Java tasks

Back to the full Java cheat sheet