Categories: Java

Naming a Thread in Java

  • The Thread class offers ways to modify and retrieve a thread’s name.
  • Each thread by default has a name, such as thread-0, thread-1, and so forth.
  • By using the setName() method, we can modify the thread’s name.
  • Below is the syntax for the setName() and getName() methods:

Syntax:

public String getName()  
public void setName(String name)
Example : Using setName() Method

class Coderz extends Thread
{  
  public void run()
  {  
   System.out.println("running...");  
  }  
 
 public static void main(String args[])
 {  
// creating two threads   
// using the contructor of the class  
  Coderz ob1=new Coderz();  
  Coderz ob2=new Coderz();
// invoking the getName() method to get the names  
   
  System.out.println("Name of ob1:"+ob1.getName());  
  System.out.println("Name of ob2:"+ob2.getName());  
   
// invoking the start() method on both the threads  
  ob1.start();  
  ob2.start();  
  
  ob1.setName("Coderzy java multithreading");  
  System.out.println("After changing name of ob1:"+ob1.getName());  
 }  
}  
Output:
Name of ob1:Thread-0
Name of ob2:Thread-1
running...
running...
After changing name of ob1:Coderzy java multithreading
Example : Without Using setName() Method
import java.io.*;  
  
// The ThreadNameClass is the child class of the class Thread  
class ThreadName extends Thread  
{  
  
// constructor of the class  
ThreadName(String threadName)  
{  
// invoking the constructor of  
// the superclass, which is Thread class.  
super(threadName);  
}  
  
// overriding the method run()  
public void run()  
{  
System.out.println(" The thread is running....");  
}  
}  
  
public class ThreadNamingExample  
{  
// main method  
public static void main (String argvs[])  
{  
// creating two threads and settting their name  
// using the contructor of the class  
ThreadName th1 = new ThreadName("Coderz1");  
ThreadName th2 = new ThreadName("Coderz2");  
  
// invoking the getName() method to get the names  
// of the thread created above  
System.out.println("Thread - 1: " + th1.getName());  
System.out.println("Thread - 2: " + th2.getName());  
  
  
// invoking the start() method on both the threads  
th1.start();  
th2.start();  
}  
}  
Output:
Thread - 1: Coderz1
Thread - 2: Coderz2
 The thread is running....
 The thread is running....
Current Thread:

A reference to the thread that is currently running is returned by the currentThread() method.

Syntax:
public static Thread currentThread()    
Example : Using currentThread() method:
public class Coderz extends Thread
{  
 public void run()
 {  
        // Getting the current thread name
        // using getname() method
  System.out.println(Thread.currentThread().getName());  
 }  
 public static void main(String args[])
 {  
     // Creating two threads inside main() method
  Coderz t1=new Coderz();  
  Coderz t2=new Coderz();  
  
  t1.start();  
  t2.start();  
 }  
}  
Output:
Thread-0
Thread-1

Note: also read about the Thread.sleep() method in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago