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.


PROGRAM
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());
 }

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

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.