Start named virtual thread

Glavo zjx001202 at gmail.com
Wed Jul 26 19:13:56 UTC 2023


Sorry, I reread the virtual thread JEP and found that I can use
`Thread.Builder::unstarted`
to create virtual threads that have not been started, and then I can do
what I want to do.

Sorry for wasting your time.


Glavo

On Thu, Jul 27, 2023 at 2:40 AM Alan Bateman <Alan.Bateman at oracle.com>
wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230727/db9a4ae9/attachment.htm>


More information about the loom-dev mailing list