Java / Java 21 Virtual Threads Interview questions
What is the default naming behavior for virtual threads?
Unlike platform threads, which the JVM auto-names something like Thread-0, Thread-1, and so on, virtual threads are unnamed by default - their name is an empty string.
This matters because with potentially millions of short-lived virtual threads, generic incrementing names would add little debugging value and some overhead. You explicitly opt in to a name when it helps you.
Thread.ofVirtual().name("order-worker-", 0).start(() -> handle());
The overload above appends an auto-incrementing counter to the given prefix, which is handy for distinguishing many similar virtual threads in logs or thread dumps.
More Related questions...