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

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago