Thread Name In Java Example
Chapter:
Thread
Last Updated:
13-06-2017 14:56:38 UTC
Program:
/* ............... START ............... */
public class JavaThreadName {
public static void main(String[] args) {
// get currently running thread object
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
currentThread.setName("Java New ");
System.out.println("Thread Name : " + currentThread.getName());
}
}
/* ............... END ............... */
Output
Thread[main,5,main]
Thread Name : Java New
Notes:
-
The Thread class provides methods to change and get the name of a thread. By default, each thread has a name i.e. thread-0, thread-1 and so on. By we can change the name of the thread by using setName() method.
- java.lang.Thread.getName() method returns this thread's name.
- Syntax : public final String getName().
- java.lang.Thread.setName() method changes the name of thread to argument name.
- Syntax : public final void setName(String name).
Tags
Set Thread Name, Java