.Thread.currentThread().getName() is "" for a virtual thread

Lutz Horn lutz.horn at posteo.de
Tue May 17 09:25:26 UTC 2022


Hi,

using non-virutal threads, `Thread.currentThread().getName()` can be 
used to get the name of the current thread. This has a sensible value 
for threads managed by, for example, a `Executors.newFixedThreadPool(256)`.

Calling `.getName()` on a virtual thread managed by a 
`Executors.newVirtualThreadPerTaskExecutor()` returns the empty string "".

Doing without `.getName()` in this szenario, 
`Thread.currentThread().toString()` returns something like

"VirtualThread[#96]/runnable at ForkJoinPool-1-worker-3"

which is fine but a little long for use cases such as logging.

What is the recommended way to get a short string representation of a 
virtual thread?

Regards

Lutz



More information about the loom-dev mailing list