Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Sunday, September 1, 2024

Java Thread.join() Examples to Wait for Another Thread

1. Introduction


In this tutorial, We'll learn how to use Thread.join() method in java. And also how to join the multiple threads at one place after completing the execution of all threads or one thread or any other threads.

join() method is part of the Thread class and it is part of the java.lang package. All the classes in java.lang package is not needed to add import statements. So, directly you can use it in the programs. join() is mainly used to sleep or wait the current thread until completion of another thread execution.

Similar to the wait() and notify(), join() also widely used in the thread intercommunication.

2. Thread.join() Syntax


below is the syntax from Thread API. join() method is an overloaded method so we should be careful which method should be used.

public final void join() throws InterruptedException

public final void join(long millis) throws InterruptedException

public final void join(long millis, int nanos) throws InterruptedException


Java 8 String API also has join() method.


3. Thread.join() Example To Wait For Another Thread Execution Completion

When a Thread.join() method is invoked then the current thread will go into the waiting state. Once invoked thread completes it's execution then-current thread comes from out of waiting state.

For example. we have 3 threads such as t1, t2, t3. All will be created and called start() from main method. So, here the main() method creates the main thread. Now, our goal is to execute first t1, next t2, and last t3.

We can use join() method to wait main thread untill completion of the t1 thread execution. After that start t2 thread and next start t3 thread.

See the below example program that tries to print numbers by each thread as below.

thread 1 print from 1 to 10
thread 2 print from 11 to 20
thread 3 print from 21 to 30

package com.javaprogramto.threads;

public class ThreadJoinExample {

 public static void main(String[] args) throws InterruptedException {
  System.out.println("main thread started execution. Current thread name : " + Thread.currentThread().getName());
  PrintNumbers t1 = new PrintNumbers(1, 10);
  PrintNumbers t2 = new PrintNumbers(11, 20);
  PrintNumbers t3 = new PrintNumbers(21, 30);

  t1.start();
  t1.join();

  t2.start();
  t2.join();

  t3.start();
  t3.join();

  System.out.println("All threads completed excution.");
  System.out.println(Thread.currentThread().getName() + " complete execution");
 }

}

class PrintNumbers extends Thread {

 private int start;
 private int end;

 public PrintNumbers(int start, int end) {
  this.start = start;
  this.end = end;
 }

 @Override
 public void run() {
  for (int i = start; i <= end; i++) {
   System.out.println(Thread.currentThread().getName() + " - " + i);
  }
  System.out.println(Thread.currentThread().getName() + " thread execution completed.");
 }
}

Output:

main thread started execution. Current thread name : main
Thread-0 - 1
Thread-0 - 2
Thread-0 - 3
Thread-0 - 4
Thread-0 - 5
Thread-0 - 6
Thread-0 - 7
Thread-0 - 8
Thread-0 - 9
Thread-0 - 10
Thread-0 thread execution completed.
Thread-1 - 11
Thread-1 - 12
Thread-1 - 13
Thread-1 - 14
Thread-1 - 15
Thread-1 - 16
Thread-1 - 17
Thread-1 - 18
Thread-1 - 19
Thread-1 - 20
Thread-1 thread execution completed.
Thread-2 - 21
Thread-2 - 22
Thread-2 - 23
Thread-2 - 24
Thread-2 - 25
Thread-2 - 26
Thread-2 - 27
Thread-2 - 28
Thread-2 - 29
Thread-2 - 30
Thread-2 thread execution completed.
All threads completed excution.
main complete execution

Note:

1. If the thread is interrupted then join() method may also return from the waiting state and throws an interrupted exception. But this happens in a rare case.

2. If the thread is not started and if we call join() method that does not make the main thread to wait. because the thread is not started so it will not wait and go to the next statement execution t2.start().

//t1.start();
t1.join();

t2.start();
t2.join();

//t3.start();
t3.join();

4. Thread.join(long) with timeout in milliseconds


This is also the most used method in some scenarios. If the main thread is calling t2.join() and the main thread is waiting to finish t2 execution. But, thread t2 execution takes more time than expected. So, we can specify the time x in milliseconds to join the method so that the main thread will wait for x seconds and then come out from the waiting state.

Syntax:

public final void join(long millis) throws InterruptedException

join(long timeout) Example:


t1.start();
t1.join(500);

t2.start();
t2.join(500);

t3.start();
t3.join(500);

5. Conclusion


In this article, We've seen how to use Thread.join() method with examples.

GitHub

Thread join() Examples

No comments:

Post a Comment

Please do not add any spam links in the comments section.