Concurrent HTTP with virtual threads
Fetch many URLs concurrently with virtual threads and HttpClient.
Porównanie kodu
✕ Java 8
ExecutorService pool =
Executors.newFixedThreadPool(10);
List<Future<String>> futures =
urls.stream()
.map(u -> pool.submit(
() -> fetchUrl(u)))
.toList();
// manual shutdown, blocking get()
✓ Java 21+
try (var exec = Executors
.newVirtualThreadPerTaskExecutor()) {
var results = urls.stream()
.map(u -> exec.submit(
() -> client.send(req(u),
ofString()).body()))
.toList().stream()
.map(Future::join).toList();
}
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
Thread per request
No pool sizing — one virtual thread per URL.
Simple code
Write straightforward blocking code.
High throughput
Thousands of concurrent requests with minimal resources.
Stare podejście
Thread Pool + URLConnection
Nowoczesne podejście
Virtual Threads + HttpClient
Od JDK
21
Poziom trudności
Średniozaawansowany
Wsparcie JDK
Concurrent HTTP with virtual threads
Dostępne
Widely available since JDK 21 LTS (Sept 2023)
Jak to działa
Virtual threads make it practical to create a thread per HTTP request. Combined with HttpClient, this replaces complex async callback patterns with simple blocking code that scales.
Powiązana dokumentacja