The join method in threading

Thread class is having a join() method. Assume from thread ‘Thread1’   we are calling ‘Thread2.join()’. In this case the calling thread (‘Thread1’) is waiting to get the ‘Thread2’ to get completed and there after ‘Thread1’ resumes its execution.If we specify ‘Thread2.join(long interval)’ then the parameter interval is the maximum time by which the ‘Thread1’ can stay as blocked(Before coming back to runnable). The idea of join() will become clear once we discuss an example.

The join method in threading – Example

We have two threads .First thread is printing multiples of 10 from 0 to 100 and second thread is printing multiples of 10 from 100 to 200.We need to join second thread  behind first thread so that multiples of 10 from 0 to 200 are printing.Now let us see the first thread.

public class FirstThread extends Thread {
int number = 0;
public FirstThread(String name) {
super(name);
}
public void run() {
while (number < 100) { System.out.println("Number in " + Thread.currentThread().getName() + " is " + number); try { sleep(1000); number = number + 10; } catch (InterruptedException e) { e.printStackTrace(); } } } }

Now let us see the second thread. It contains a reference to the first thread.In the run method we are calling the join method.So it is not going to print any number till the end of execution of first thread.After the end of execution of first thread is over , second thread is starting to print its numbers.

public class SecondThread extends Thread {
int number = 100;
Thread firstThread = null;
public SecondThread(String name, Thread first) {
super(name);
firstThread = first;
}
public void run() {
try {
firstThread.join();
System.out.println("Joining second thread now");
while (number <= 200) { System.out.println("Number in " + Thread.currentThread().getName() + " = " + number); sleep(1000); number = number + 10; } } catch (InterruptedException e) { e.printStackTrace(); } } }

Now let us see the main class ,It simply creates objects of the above classes.Second thread is having a reference to the first thread so that it can call the join() on it. Remember , if we are using this method , we should handle the InterruptedException too.

public class Main {
public Main(){
}
public static void main(String[]args){
FirstThread firstThread = new FirstThread("firstThread");
SecondThread secondThread = new SecondThread("secondThread",firstThread);
firstThread.start();
secondThread.start();
}
}

Now let us verify the output.The output clearly shows us how join() works.Multiples of 10 from 0 to 100 is displaying first and then from 100 to 200.Thread name also displayed, so it is very clear now.

Output

Number  in firstThread = 0

Number  in firstThread = 10

Number  in firstThread = 20

Number  in firstThread = 30

Number  in firstThread = 40

Number  in firstThread = 50

Number  in firstThread = 60

Number  in firstThread = 70

Number  in firstThread = 80

Number  in firstThread = 90

Joining second thread now

Number  in secondThread = 100

Number  in secondThread = 110

Number  in secondThread = 120

Number  in secondThread = 130

Number  in secondThread = 140

Number  in secondThread = 150

Number  in secondThread = 160

Number  in secondThread = 170

Number  in secondThread = 180

Number  in secondThread = 190

Number  in secondThread = 200

See Related Discussions

Threading Basics

Thread Safety in Java

Thread Communication in Java

Thread Priorities in Java

The yield() method

The sleep() method

Daemon Threads in Java

Thread pool executor in java

Thread pools in Java

Thread Dead lock in Java

Thread Live lock in Java

2 thoughts on “The join method in threading

  1. Brand Reply

    That is really nice concept dude.iam genuinely proud of you . Do u have twitter?? i want to adhere to you .thx

Leave a Reply

Your email address will not be published. Required fields are marked *