Virtual thread executor
Use virtual thread executors for unlimited lightweight concurrency.
Porównanie kodu
✕ Java 8
ExecutorService exec =
Executors.newFixedThreadPool(10);
try {
futures = tasks.stream()
.map(t -> exec.submit(t))
.toList();
} finally {
exec.shutdown();
}
✓ Java 21+
try (var exec = Executors
.newVirtualThreadPerTaskExecutor()) {
var futures = tasks.stream()
.map(exec::submit)
.toList();
}
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
No sizing
No pool size to tune — create as many threads as needed.
Lightweight
Virtual threads use KB of memory, not MB.
Auto-closeable
try-with-resources handles shutdown automatically.
Stare podejście
Fixed Thread Pool
Nowoczesne podejście
Virtual Thread Executor
Od JDK
21
Poziom trudności
Średniozaawansowany
Wsparcie JDK
Virtual thread executor
Dostępne
Widely available since JDK 21 LTS (Sept 2023)
Jak to działa
The virtual thread executor creates a new virtual thread for each task. No pool sizing needed — virtual threads are cheap enough to create millions of them.
Powiązana dokumentacja