Sunday 29 January 2012

join() method in Java Thread with Examples

The join() method of a Thread instance can be used to "join" the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended. If join() is called on a Thread instance t, the currently running thread will block until the thread instance t has finished execution. 
See one example 
class MyThread extends Thread {

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("From mythread i=" + i);
        }
    }
}

public class Main {
    public static void main(String args[]) {
        MyThread mythread = new MyThread();
        System.out.println("Before starting mythread");

        mythread.start();
        try {
            mythread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("After finishing mythread");


        System.out.println("Is Main Thread Running "
                + Thread.currentThread().isAlive());
        System.out.println("Is mythread Thread Running " + mythread.isAlive());


    }
}


The output is given below

Before starting mythread
From mythread i=1
From mythread i=2
From mythread i=3
From mythread i=4
From mythread i=5
After finishing mythread
Is Main Thread Running true
Is mythread Thread Running false

We can check the above output step by step.
  1. Main thread started.
  2. Main thread printed the message ' Before starting mythread'.
  3. Main thread created a child thread named mythread and started it.
  4. Main thread executed the statement 'mythread.join()'
  5. Main thread stopped execution until mythread finishes its execution.
  6. mythread printed five messages.
  7. mythread finished its execution.
  8. Main thread restarted its execution.
  9. Main thread printed the message 'After finishing mythread'.
You can see that after executing the statement mythread.join(), main thread stopped its execution until mythread finished its work.


The join method is available in two other formats also

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

public final void join(long millis) throws InterruptedException 


If join(millis) is called on a Thread instance t, the currently running thread will block at most millis milliseconds for thread t to die.
i.e. currently running thread waits until the time,thread t finishes its work or specified millis milliseconds whichever is minimum.


See the example given below


import java.util.*;

class MyThread extends Thread {

    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("From mythread i=" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Main {
    public static void main(String args[]) {
        MyThread mythread = new MyThread();
        mythread.start();
        System.out.println("blocked main thread execution" + new Date());
        try {
            mythread.join(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("unblocked main thread execution" + new Date());

        System.out.println("Is Main Thread Running "
                + Thread.currentThread().isAlive());
        System.out.println("Is mythread Thread Running " + mythread.isAlive());

    }
}

The output is given below

From mythread i=1
blocked main thread execution Mon Jan 30 23:13:51 IST 2012
From mythread i=2
From mythread i=3
From mythread i=4
unblocked main thread execution Mon Jan 30 23:13:54 IST 2012
Is Main Thread Running true
Is mythread Thread Running true
From mythread i=5


You can see that,main thread stopped execution after executing the statement mythread.join(3000) and restarted execution after 3 seconds even then mythread execution is not died(see example, mythread.isAlive() is returning true).
 

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


This method is same as the above one ,only difference is that you can specify the timeout value more accurately,in the order of nano seconds.


The join method throws InterruptedException, if another thread has interrupted the current thread.So you must keep the join method within InterruptedException.

No comments:

Post a Comment