Program for Naming a Thread

This program demonstrates you how to change the thread name. The setName(String) method of Thread class is used to set the name of thread.

public class NamingThread {

 public static void main(String[] args) {
  Thread thread = Thread.currentThread();
  System.out.println("Main thread's original name is : "+thread.getName());
  thread.setName("The Main Thread");
  System.out.println("Main thread's new name is : "+thread.getName());

C:\>java NamingThread
Main thread's original name is : main
Main thread's new name is : The Main Thread

No comments:

Post a comment