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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago