Java Thread SetName() Method
Chapter:
Thread
Last Updated:
19-08-2017 08:40:48 UTC
Program:
/* ............... START ............... */
public class JavaThreadsetName {
public static void main(String[] args) {
Thread t = Thread.currentThread();
// prints the thread name
System.out.println("Thread = " + t);
// change the thread name
t.setName("Admin Thread");
// prints the thread after changing name
System.out.println("Thread after changing name = " + t);
int count = Thread.activeCount();
System.out.println("currently active threads = " + count);
}
}
/* ............... END ............... */
Output
Thread = Thread[main,5,main]
Thread after changing name = Thread[Admin Thread,5,main]
currently active threads = 1
Notes:
-
The java.lang.Thread.setName() method changes the name of this thread to be equal to the argument name.
Tags
Thread SetName() Method, Java, Thread