Home »
Java programming language
Java Thread Class final void join(long time_in_ms, int time_in_ns) method with Example
Java Thread Class final void join(long time_in_ms, int time_in_ns) method: Here, we are going to learn about the final void join(long time_in_ms, int time_in_ns) method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 26, 2019
Thread Class final void join(long time_in_ms, int time_in_ns)
- This method is available in package java.lang.Thread.join(long time_in_ms, int time_in_ns).
- join(long time_in_ms, int time_in_ns) method is applicable when currently executing thread wants to wait for a particular amount of time in milliseconds with additional time also in nanoseconds until completing some other thread then we should go for join(long time_in_ms, int time_in_ns) method of Thread class.
- This method is not static so we cannot access this method with the class name too.
- This method is final we can't override this method in child class.
- The return type of this method is void so it does not return anything.
- This method throws an InterruptedException, so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.
For example, We have two threads [t1 – PreparedExamPaper], [t2 – PrintingExamPaper] so will see what will happen.
Let suppose if a thread t1 executes, t2.join(1000, 500) then thread t1 will entered into waiting state for 1000 milliseconds + 500 nanoseconds until t2 completes and suppose in case if t2 couldn't complete its execution in 1000 ms + 500 ns so in that case, t1 will get a chance to execute and if thread t1 goes into waiting state or sleep mode then again t2 will get a chance to execute its execution for 1000 ms + 500 ns and same process will repeat.
Syntax:
final void join(long time_in_ms, int time_in_ns){
}
Parameter(s):
When we write t2.join(2000, 1000), so this line means currently executing thread will stop its execution for 2000 milliseconds +1000 nanoseconds until t2 completion.
Return value:
The return type of this method is void, it does not return anything.
Java program to demonstrate example of join(long time_in_ms, int time_in_ns) method
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class MyThread extends Thread {
//Override run() method of Thread class
public void run() {
for (int i = 0; i < 5; ++i) {
System.out.println("Thread started:" + Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
System.out.println("Thread Ended :" + Thread.currentThread().getName());
}
}
class MainThread1 {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
mt.start();
/* Note -1*/
mt.join(1000, 500);
for (int j = 0; j < 2; ++j)
System.out.println("Thread started:" + Thread.currentThread().getName());
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
Note1: Here we have written /*mt.join(1000,500)*/ means currently executing thread [main] will give a chance to another thread named [MyThread mt] for 1000 ms +500 ns and then after main thread will get a chance to execute and if main thread goes into waiting for state or sleep mode then again MyThread will get a chance for 1000 ms +500 ns and this repeats until complete execution of MyThread.
Output
E:\Programs>javac MainThread1.java
E:\Programs>java MainThread1
Thread started:Thread-0
Thread started:Thread-0
Thread started:main
Thread started:main
Thread started:Thread-0
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0