A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Getting and Setting Name of a Thread

Getting and Setting Name of a Thread

How to get name of a thread? How to set name of a thread?

Every thread in java has some name, it may be default name provided by JVM or by a Programmer.
There are two methods in java to get and set name of a thread:


  1. public final string getName

  2. public final void setName(String Name)


class myThread extends Thread
{
    public void run()
    {
        for(int i=0; i<10; i++)
        {
            System.out.println("child thread");
        }
    }
}

class ThreadGettingName
{
    public static void main(String [] args)
    {
        System.out.println("Current Thread Name : "+Thread.currentThread().getName());
        myThread t = new myThread();
        System.out.println(t.getName());
        Thread.currentThread().setName("Nitu thread");
        System.out.println("Current Thread Name : "+Thread.currentThread().getName());
       
    }

}

Sample output:


LEARN TUTORIALS

.

.