Java/J2EE and SOA (239 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (46 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (239 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Join method in Java: How to join threads?

Published on Sep 10,2019 33 Views
Aayushi Johari
A technophile who likes writing about different technologies and spreading knowledge. A technophile who likes writing about different technologies and spreading knowledge.
54 / 90 Blog from Java Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

A thread in Java is considered to be a thread of execution in a program. Java.lang.thread includes various methods which help in running multiple threads concurrently. One of the commonly used method is the Join Method in Java. Let’s explore this method in the below sequence.


Let’s get started.

What is a Join Method in Java?

Join method in Java allows one thread to wait until another thread completes its execution. In simpler words, it means it waits for the other thread to die. It has a void type and throws InterruptedException. Joining threads in Java has three functions namely,

  • join()
  • join(long millis)
  • join(long millis, int nanos)
MethodDescription

join()

Waits for this thread to die

join(long millis)

Waits at most millis milliseconds for this thread to die

join(long millis, int nanos)

Waits at most millis milliseconds plus nano nanoseconds for this thread to die

Syntax:

  • public final void join()
  • public final void join(long millis, int nanos)
  • public final void join(long millis)

 

Java Program to implement Thread.join Method

Let’s implement all the joins in Java one by one.

 

Example of join() Method in Java

package Edureka;

import java.io.*; 
import java.util.*; 

public class Threadjoiningmethod extends Thread{  
		 public void run(){  
		  for(int i=1;i<=4;i++){  
		   try{  
		    Thread.sleep(500);  
		   }catch(Exception e){System.out.println(e);}  
		  System.out.println(i);  
		  }  
		 }  
		public static void main(String args[]){  
			Threadjoiningmethod th1=new Threadjoiningmethod ();  
			Threadjoiningmethod th2=new Threadjoiningmethod ();  
			Threadjoiningmethod th3=new Threadjoiningmethod ();  
		 th1.start();  
		 try{  
		  th1.join();  
		 }
		 catch(Exception e){
			 System.out.println(e);
			 }  
		  
		 th2.start();  
		 th3.start();  
		 }  
		}

Output:

1
2
3
4
1
1
2
2
3
3
4
4

Explanation: Here you can notice thread1 first complete its task, then thread2 and thread3 will execute.

 

Example of join(long millis) Method in Java

package Edureka;

import java.io.*; 
import java.util.*; 

public class Threadjoiningmethod extends Thread{  
		 public void run(){  
		  for(int i=1;i<=4;i++){  
		   try{  
		    Thread.sleep(200);  
		   }catch(Exception e){System.out.println(e);}  
		  System.out.println(i);  
		  }  
		 }  
		public static void main(String args[]){  
			Threadjoiningmethod th1=new Threadjoiningmethod();  
			Threadjoiningmethod th2=new Threadjoiningmethod();  
			Threadjoiningmethod th3=new Threadjoiningmethod();  
		 th1.start();  
		 try{  
		  th1.join(1000);  
		 }
		 catch(Exception e){
			 System.out.println(e);
			 }  
		  
		 th2.start();  
		 th3.start();  
		 }  
		}

 

Output:

1
2
3
4
1
1
2
2
3
3
4
4

Explanation: Here you can notice thread1 completes its task for 200millisecond(4 times as sleep time is 200),  then thread2 and thread3 will execute.

Thus we have come to an end of this article on ‘Join method in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog “Join method in Java”  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.