During doing a tutorial about JDK11 HttpClient
, using a https://httpstat.us/500?sleep=1000
endpoint which is returning HTTP 500
after 1 second, I prepared the following piece of code:
HttpClient client = HttpClient.newHttpClient();
var futures = Stream.of(
"https://httpstat.us/500?sleep=1000",
"https://httpstat.us/500?sleep=1000",
"https://httpstat.us/500?sleep=1000"
).map(link -> client
.sendAsync(
newBuilder(URI.create(link)).GET().build(),
HttpResponse.BodyHandlers.discarding()
).thenApply(HttpResponse::statusCode)
).collect(Collectors.toList());
futures.stream().map(CompletableFuture::join).forEach(System.out::println);
and it is working fine. Program execution takes ~1.5s, output is being rendered in terminal at the same time for all three calls - everything is good.
But when I'm changing this to
HttpClient client = HttpClient.newHttpClient();
Stream.of(
"https://httpstat.us/500?sleep=1000",
"https://httpstat.us/500?sleep=1000",
"https://httpstat.us/500?sleep=1000"
).map(link -> client
.sendAsync(
newBuilder(URI.create(link)).GET().build(),
HttpResponse.BodyHandlers.discarding()
).thenApply(HttpResponse::statusCode)
).map(CompletableFuture::join).forEach(System.out::println);
it seems to not be working async anymore - three 500
are being shown one by one with 1 second delay before each.
Why? What am I missing here?