Start named virtual thread

Alan Bateman Alan.Bateman at oracle.com
Wed Jul 26 18:40:32 UTC 2023


On 26/07/2023 18:44, Glavo wrote:
> Sorry I didn't understand `Thread.Builder` correctly.
>
> Still, I'd be happier if we could provide the convenience method above.
>
> I'm just developing a library and want to get an ExecutorService that 
> names the virtual threads with my strategy.
> Achieving this was a bit of a pain, especially since I had to use 
> reflection to call the thread builder API for Java 8 compatibility.
> I'd like to have a simple factory method instead of using the thread 
> builder API unnecessarily.
>
It sounds like you are looking for:

ThreadFactory factory = Thread.ofVirtual().name("duke-", 0).factory();
var executor = Executors.newThreadPerTaskExecutor(factory);

which will name the threads duke-0, duke-1, duke-2, ...  I don't think a 
strong case for introducing a new factory method here, the issue sounds 
more like it's more code using core reflection.

-Alan


More information about the loom-dev mailing list