The Thread getName method is used to retrieve the name of the thread that the associated thread object represents. This method returns a string datatype that represents the name of the thread.
Example 1:
Thread thread = new Thread(() -> { System.out.println(Thread.currentThread().getName()); }); thread.start();
Output: The output of this code will be the name of the thread, which is assigned by default as "Thread-0".
Example 2:
Thread thread = new Thread(() -> { System.out.println(Thread.currentThread().getName()); });
thread.setName("CustomThread"); thread.start();
Output: The output will be the name of the thread that has been set by the user, which in this case is "CustomThread".
Package Library: The Thread class is part of the java.lang package library.
Java Thread.getName - 30 examples found. These are the top rated real world Java examples of Thread.getName extracted from open source projects. You can rate examples to help us improve the quality of examples.