Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Thread.getName — reading a thread's name

Thread.getName() returns the specified thread's name as a String. The method takes no arguments and does not start the thread.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public final String getName()
thread.getName()

Thread.getName is a public instance method with the signature public final String getName(). Calling thread.getName() reads the name of thread, not necessarily the thread executing the code. For the current thread, use Thread.currentThread().getName().

Platform threads receive generated names by default; unnamed virtual threads return the empty string "". A name can change through setName(String), so do not treat it as an immutable identifier.

The example explicitly assigns the name worker-1 and reads it without calling start(). The expected result therefore does not depend on default naming or execution scheduling.

Examples

public class Main {
    public static void main(String[] args) {
        Thread worker = new Thread(() -> {}, "worker-1");
        System.out.println(worker.getName());
    }
}

Result

worker-1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources