Named virtual threads

Danish Nawab dnawab at outlook.com
Tue Feb 6 08:49:39 UTC 2024


Javadoc [1] has the following to say about virtual threads:

> Virtual threads do not have a thread name by default. The getName<https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#getName()> method returns the empty string if a thread name is not set.

What's the reason for this difference between platform and virtual threads? Is this because virtual threads need to be so cheap and lightweight to create and retain that the performance hit incurred when creating a name (an atomic add and a string concat [2]) is deemed unacceptable?
The resulting string will also occupy some heap space (which might not be that big of a problem considering the recommended use of virtual threads is to "be short-lived and have shallow call stacks" [3]).

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 puts the onus on the users of virtual threads (often frameworks and libraries) to name their virtual threads [4] [5]. This of course begs the question of what downsides (performance hits etc.) these libraries will impose on their users by naming virtual threads when the JDK decided not to do so.
Stating the reasons why this decision was made (and will be retained?) would help the community decide what the best course of action is here.

[1] https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html
[2] https://github.com/openjdk/jdk21/blob/master/src/java.base/share/classes/java/lang/ThreadBuilders.java#L315-L321
[3] https://openjdk.org/jeps/444
[4] https://github.com/jetty/jetty.project/issues/11353
[5] https://github.com/apache/tomcat/blob/main/java/org/apache/tomcat/util/threads/VirtualThreadExecutor.java#L40

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


More information about the loom-dev mailing list