I'm trying the new HTTP client API from JDK 11, specifically its asynchronous way of executing requests. But there is something that I'm not sure I understand (sort of an implementation aspect). In the documentation, it says:
Asynchronous tasks and dependent actions of returned
CompletableFuture
instances are executed on the threads supplied by the client'sExecutor
, where practical.
As I understand this, it means that if I set a custom executor when creating the HttpClient
object:
ExecutorService executor = Executors.newFixedThreadPool(3);
HttpClient httpClient = HttpClient.newBuilder()
.executor(executor) // custom executor
.build();
then if I send a request asynchronously and add dependent actions on the returned CompletableFuture
, the dependent action should execute on the specified executor.
httpClient.sendAsync(request, BodyHandlers.ofString())
.thenAccept(response -> {
System.out.println("Thread is: " + Thread.currentThread().getName());
// do something when the response is received
});
However, in the dependent action above (the consumer in thenAccept
), I see that the thread doing it is from the common pool and not the custom executor, since it prints Thread is: ForkJoinPool.commonPool-worker-5
.
Is this a bug in the implementation? Or something I'm missing? I notice it says "instances are executed on the threads supplied by the client's Executor, where practical", so is this a case where this is not applied?
Note that I also tried thenAcceptAsync
as well and it's the same result.