Named virtual threads

Alan Bateman Alan.Bateman at oracle.com
Tue Feb 6 11:47:31 UTC 2024


On 06/02/2024 08:49, Danish Nawab wrote:
> :
>
> I ask because, in the absence of a thread name, the observability of 
> the application suffers. Popular libraries like logback only include 
> the thread name (by default) in the logs which makes the log messages 
> less useful depending on the issue to debug. Similarly, thread dumps 
> don't contain thread names anymore.
> Of course, virtual threads do have a |threadId|​ but that is not as 
> discoverable as the name.
>
This has come up a few times, you'll need to go through the archives to 
see previous discussions on the topic.

To summarize: Virtual threads are intended to be low footprint so they 
don't get an automatically generated name by default. They have a thread 
ID of course and that thread ID is in the string representation and is 
readily available to logging libraries with Thread::threadId. Virtual 
threads can be named where it makes sense. In an application with 
thousands of virtual threads then it may be useful to name a few special 
threads, like the thread that is accepting connections but less useful 
to name the 10_000 threads in the same "job role" handling a specific 
request. Thread dumps include the thread ID for each thread and will 
include the names of threads have have been given a name.

I assume you've already found that you can name threads with the 
Thread.Builder API and this includes auto-numbering. Many frameworks and 
libraries use a ThreadFactory to create unstarted threads and that can 
be used to put initial names on virtual threads too.

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20240206/c3a2360f/attachment.htm>


More information about the loom-dev mailing list