Categories: Java

Java join() method

join() method:

The join() method of the Java.lang.Thread class enables one thread to wait until another thread has finished running. If t is a Thread object that is currently running, t.join() will ensure that t is terminated before the program executes the following instruction.


If more than one thread uses the join() method, overloading on join enables the programmer to set a waiting time. It would help if you didn’t assume that join will wait as long as you specify because, like sleep, join depends on the OS for timing.

There are three overloaded join functions.

1)join(): Until the thread on which it is called is dead, it will put the active thread on hold. When a thread is interrupted, InterruptedException is thrown.


Syntax:

public final void join()

2)join(long millis): The current thread will be placed on hold until the thread on which it is called is dead or until a certain amount of time has passed (milliseconds).

Syntax:

public final synchronized void join(long millis)

3)join(long millis, int nanos): It will put the current thread on wait until the thread on which it is called is dead or wait for a specified time (milliseconds + nanos).

Syntax:

public final synchronized void join(long millis, int nanos)
Example 1:

import java.io.*;
class ThreadJoining extends Thread
{
 @Override
 public void run()
 {
  for (int i = 0; i < 2; i++)
  {
   try
   {
    Thread.sleep(500);
    System.out.println("Current Thread: "
      + Thread.currentThread().getName());
   }

   catch(Exception ex)
   {
    System.out.println("Exception has" +
        " been caught" + ex);
   }
   System.out.println(i);
  }
 }
}

class Coderz
{
 public static void main (String[] args)
 {

  ThreadJoining t1 = new ThreadJoining();
  ThreadJoining t2 = new ThreadJoining();
  ThreadJoining t3 = new ThreadJoining();
  t1.start();

  try
  {
   System.out.println("Current Thread: "
    + Thread.currentThread().getName());
   t1.join();
  }

  catch(Exception ex)
  {
   System.out.println("Exception has " +
        "been caught" + ex);
  }

  
  t2.start();
  try
  {
   System.out.println("Current Thread: "
    + Thread.currentThread().getName());
   t2.join();
  }

  catch(Exception ex)
  {
   System.out.println("Exception has been" +
         " caught" + ex);
  }

  t3.start();
 }
}
Output:
Current Thread: main
Current Thread: Thread-0
0
Current Thread: Thread-0
1
Current Thread: main
Current Thread: Thread-1
0
Current Thread: Thread-1
1
Current Thread: Thread-2
0
Current Thread: Thread-2
1
Example 2:

class TestJoinMethod2 extends Thread{    
 public void run(){    
  for(int i=1;i<=5;i++){    
   try{    
    Thread.sleep(500);    
   }catch(Exception e){System.out.println(e);}    
  System.out.println(i);    
  }    
 }    
public static void main(String args[]){    
 TestJoinMethod2 t1=new TestJoinMethod2();    
 TestJoinMethod2 t2=new TestJoinMethod2();    
 TestJoinMethod2 t3=new TestJoinMethod2();    
 t1.start();    
 try{    
  t1.join(1500);    
 }catch(Exception e){System.out.println(e);}    
    
 t2.start();    
 t3.start();    
 }    
}    
Output:
1
       2
       3
       1
       4
       1
       2
       5
       2
       3
       3
       4
       4
       5
       5

Note: also read about the Creating a Thread 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…

2 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…

1 year ago

DSA: Trie

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

1 year ago

Trees: Lowest Common Ancestor

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

1 year ago