Java Thread getName() Method
Example
Get the name of a new thread and of the main thread:
public class Main {
public static void main(String[] args) {
Thread myThread = new Thread();
myThread.setName("Thread 1");
System.out.println(myThread.getName());
Thread mainThread = Thread.currentThread();
System.out.println(mainThread.getName());
}
}
Definition and Usage
The getName() method returns the name of a thread.
Every thread has a name. The thread that runs the main() method is called main. A new thread gets a name like Thread-0, unless you give it a name with setName().
Tip: Use the setName() method to change the name of a thread.
Syntax
public final String getName()
Parameter Values
None.
Technical Details
| Returns: | The name of the thread, as a String. |
|---|
Related Pages
❮ Thread Methods