Friday, 5 May 2017

11. Thread join()


1. Thread class join()  usage
1.       If a thread wants to wait until completing some other thread then we should go for join().
2.       For example, if a thread t1 wants to wait until completing t2 then t1 has to call t2.join().
3.       If t1 executes t2.join(() then immediately t1 will be entered into waiting state until t2 completes.
4.       Once t2 completes then t1 can continue its execution.
5.       Example , they are 3 threads like
a.       Venue fixing – t1
b.      Wedding card printing – t2
c.       Wedding card distribution – t3
6.       t2 has to wait until t1 completion, hence t2 has to call t1.join();
7.       t3 has to wait until t2 completion , hence t3 calls t2.join();
8.       Types of join() in Thread class
a.       Public   final   void    join(); throws InterruptedException
b.      Public   final   synchronized void   join(long milliseconds); throws InterruptedException
c.        Public   final   synchronized  void  join(long milliseconds, int nanoseconds); throws InterruptedException
9.       Every join() throws InterruptedException, it is a checked exception. So compulsory we handle this exception either by using try catch or by throes keyword , otherwise we will get compile time error.
10.   Once thread in wait state will never comes to Running state directly
11.   Thread in waiting state will come to ready state in following cases
a.       Joined thread if completes
b.      Joined thread time expires
c.       InterruptedException occurs
12.   How child thread waits until main thread completion, because child thread has not contains main thread reference.
13.   For above case, we need to declare static Thread variable in child thread class, and inject main thread object in child class static Thread variable, now child thread can access main thread reference.
public class MyThread extends Thread {
  public void run() {
        for(int i=0; i<3; i++){
          System.out.println(“seetha thread”);
              try{
                    Thread.sleep(2000);
              }catch(Exception e){}
        }
  }
  public static void main(String[] args)throws Exception{
        MyThread thread = new MyThread();
        thread.start();
        thread.join();//thread.join(4000);
        for(int i=0; i<3; i++){
          System.out.println(“Rama thread”);
        }
  }
}
thread.join()
thread.join(4000);
seetha thread
seetha thread
seetha thread
seetha thread
seetha thread
Rama thread
Rama thread
Rama thread
Rama thread
Rama thread
Rama thread
seetha thread
Child thread waits until main thread completes.
public class MyThread extends Thread {
  static Thread mainThread;
  public void run() {
              try{
                    mainThread.join();
              }catch(Exception e){}
        for(int i=0; i<3; i++){
          System.out.println(“seetha thread”);
        }
  }
  public static void main(String[] args)throws Exception{
        mainThread = Thread.currentThread();
        MyThread thread = new MyThread();
        thread.start();
        for(int i=0; i<3; i++){
          System.out.println(“Rama thread”);
        }
  }
}
thread.join()
Rama thread
Rama thread
Rama thread
seetha thread
seetha thread
seetha thread
      
14.   If main thread calls join() on child thread object and child thread calls join() on main thread object then both threads will wait forever and the program will be paused , it is called  DeadLock
15.   If a thread calls join() on the same thread itself then also current thread waits until execution of same thread, it is also DeadLock.
public static void main(String[] args)throws Exception{
        Thread.currentThread().join();
  }
O/P: No output but waits .......
2. Is recommended to use Thread class join()
  1. No, don’t use join(), instead of join() we can use wait().
  2. join() will sometimes break our application functionalities.
  3. If we use join() on specific thread object, currently executing thread will go to waiting state until the the specific thread completes.
  4. But internally join() uses wait().
  1. Internal implementation of join() is
    1. join() will calls join(long millis) by passing millis as ‘0’.
    2. It will call wait() on current thread object inside a while loop.
    3. This while loop will execute up to specific thread will terminated.
    4. Once thread is going to terminated, JVM will calls notifyAll() on specific thread object.
    5. Then current thread will come to runnable state from waiting state.
    6. Now current thread will executes while loop condition, it will become false, because specific thread is dead already.
    7. Current thread will completes execution of join() and return to main application.
    8. Now remaining code of main application is executes.

No comments:

Post a Comment

3. Java Program to create Binary Tree