Home » Java programming language

Java Thread Class final synchronized void join(long time_in_ms) method with Example

Java Thread Class final synchronized void join(long time_in_ms) method: Here, we are going to learn about the final synchronized void join(long time_in_ms) method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 26, 2019

Thread Class final synchronized void join(long time_in_ms)

  • This method is available in package java.lang.Thread.join(long time_in_ms).
  • join(long time_in_ms) method is applicable when currently executing thread wants to wait for a particular amount of time in milliseconds until completing some other thread then we should go for join(long time_in_ms) method of Thread class.
  • This method is synchronized that is only one thread is allowed to operate one object.
  • 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), then thread t1 will entered into waiting state for 1000 milliseconds until t2 completes and suppose in case if t2 couldn't complete its execution in 1000 ms 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 and the same process will repeat.

Syntax:

    final synchronized void join(long time_in_ms){
    }

Parameter(s):

When we write t2.join(2000), so this line means currently executing thread will stop its execution for 2000 milliseconds 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) 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);

        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)*/ means currently executing thread [main] will give a chance to another thread named [MyThread mt] for 1000 ms 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 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 ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.