How To Provide Joining And Naming To Threads In Java

Introduction

 
In this article we discuss Joining and Naming of threads.
 

Thread join() method

 
This method waits for a thread to die. In other words, we can say it causes the currently running thread to stop executing until the thread it joins completes its task.
 
Syntax of join() public method
 
void join()

void join (long mili-seconds)
 
Let's see an example
 
In this example, when th1 completes its task th2 and th3 start executing.
 
MultithreadEx.java
  1. class MultithreadEx extends Thread {  
  2.  public void run() {  
  3.   for (int x = 1; x <= 6; x++) {  
  4.    try {  
  5.     Thread.sleep(400);  
  6.    } catch (Exception ex) {  
  7.     System.out.println(ex);  
  8.    }  
  9.    System.out.println(x);  
  10.   }  
  11.  }  
  12.  public static void main(String args[]) {  
  13.   MultithreadEx th1 = new MultithreadEx();  
  14.   MultithreadEx th2 = new MultithreadEx();  
  15.   MultithreadEx th3 = new MultithreadEx();  
  16.   th1.start();  
  17.   try {  
  18.    th1.join();  
  19.   } catch (Exception ex) {  
  20.    System.out.println(ex);  
  21.   }  
  22.   th2.start();  
  23.   th3.start();  
  24.  }  
  25. }  
Output
 
Fig-1.jpg
 
2. Example
 
In this example; we use the join(long miliseconds) method. So, when th1 completes the task for 2000 miliseconds (5 times) then th2 and th3 start executing.
 
MultithreadEx1.java
  1. class MultithreadEx1 extends Thread {  
  2.  public void run() {  
  3.   for (int x = 1; x <= 6; x++) {  
  4.    try {  
  5.     Thread.sleep(400);  
  6.    } catch (Exception ex) {  
  7.     System.out.println(ex);  
  8.    }  
  9.    System.out.println(x);  
  10.   }  
  11.  }  
  12.  public static void main(String args[]) {  
  13.   MultithreadEx1 th1 = new MultithreadEx1();  
  14.   MultithreadEx1 th2 = new MultithreadEx1();  
  15.   MultithreadEx1 th3 = new MultithreadEx1();  
  16.   th1.start();  
  17.   try {  
  18.    th1.join(2000);  
  19.   } catch (Exception ex) {  
  20.    System.out.println(ex);  
  21.   }  
  22.   th2.start();  
  23.   th3.start();  
  24.  }  
  25. }  
Output
 
Fig-2.jpg
 

How to provide naming of threads

 
The thread class provides methods to change the name of a thread.
 
Syntax
 
public String getName()
 
public void setName(String sname)
 
Example
 
In this example; we use getName(), setName() and getId() methods. This example describes how to change the name of a thread.
  1. class MultithreadEx2 extends Thread {  
  2.  public void run() {  
  3.   System.out.println("thread start running.....");  
  4.  }  
  5.  public static void main(String args[]) {  
  6.   MultithreadEx2 th1 = new MultithreadEx2();  
  7.   MultithreadEx2 th2 = new MultithreadEx2();  
  8.   System.out.println("Name of thread th1:" + th1.getName());  
  9.   System.out.println("Name of thread th2:" + th2.getName());  
  10.   System.out.println("Id of thread th1:" + th1.getId());  
  11.   th1.start();  
  12.   th2.start();  
  13.   th1.setName("Sandy");  
  14.   System.out.println("After rename of thread th1: " + th1.getName());  
  15.  }  
  16. }   
Output
 
Fig-3.jpg
 

The currentThread() method

 
This method returns a reference to the currently executing thread object.
 
Syntax
 
public static Thread currentThread()
          returns the reference of currently running thread.
 
Example
  1. class MutlithreadEx3 extends Thread {  
  2.  public void run() {  
  3.   System.out.println(Thread.currentThread().getName());  
  4.  }  
  5.  public static void main(String args[]) {  
  6.   MutlithreadEx3 th1 = new MutlithreadEx3();  
  7.   MutlithreadEx3 th2 = new MutlithreadEx3();  
  8.   th1.start();  
  9.   th2.start();  
  10.  }  
  11. }  
Output
 
Fig-4.jpg