HttpClient has no explicit way of releasing threads

Cay Horstmann cay.horstmann at gmail.com
Tue May 10 17:10:31 UTC 2022


Il 10/05/2022 15:39, Remi Forax ha scritto:
> You can change the Executor (it's one parameter of the builder) to use whatever executors you want so you can shutdown that executor as you want.
> This should fixed (1).
> 
> Also once you update to Java 19/21, it seems a good scenario to test the executor that spawn virtual threads instead of platform threads.

I am afraid build 19-ea+21-1482 doesn't yet include virtual threads, but 
it's getting close.

Be careful though. The HttpClient.Builder has an enticing method

HttpClient.Builder 	executor​(Executor executor)

which "Sets the executor to be used for asynchronous and dependent tasks."

What this means is anyone's guess, but I know from Heinz Kabutz that 
it's the executor used for the internal workings of the selector 
mechanism. See https://bugs.openjdk.java.net/browse/JDK-8204679. I don't 
think making that a virtual thread does much good.

If you want to spawn a virtual thread to process the request (a good 
idea if the processing blocks), you should use

client.sendAsync(request, responseBodyHandler)
    .thenApplyAsync(HttpResponse::body, executor);

where executor provides virtual threads.

Cheers,

Cay

-- 

Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com


More information about the core-libs-dev mailing list