Streams Średniozaawansowany

Use virtual thread executors for unlimited lightweight concurrency.

✕ 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ć.
♾️

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
Virtual thread executor
Dostępne

Widely available since JDK 21 LTS (Sept 2023)

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.

Udostępnij 𝕏 🦋 in