Java / Java 21 Coding Standards Interview Questions
Why is virtual thread naming convention different from platform thread naming?
Platform threads are few in number and long-lived, so giving each one a distinct, meaningful name like db-connection-pool-3 is cheap and genuinely useful for identifying it in a thread dump.
Thread.ofVirtual().name("order-worker-", 0).start(task); // numbered, not individually hand-named
Virtual threads are created by the millions for short-lived tasks, so hand-crafting a unique name per thread is both wasteful and unreadable in a dump full of thousands of entries; the recommended standard instead names them by role with an auto-incrementing counter, using Thread.ofVirtual().name(prefix, start), so a dump shows order-worker-482 rather than either a generic "Thread-482" or an expensively hand-built unique string.
The naming convention is really about matching the cost of the naming scheme to the thread's lifetime: a thread that lives for the length of one request needs a cheap, role-based, auto-numbered name, while a thread that lives for the life of the application can justify a fully descriptive, hand-assigned one.
More Related questions...