In Java, Thread Scheduler controls thread scheduling. But we can
use join() method on a thread to make current thread to wait for
another thread to finish.
When we use join(), the current thread stops executing. It wait for
the thread on which join() is called to finish.
This makes sure that current thread will continue only after the
thread it joined finished running. Consider following example:
Public class ThreadJoin {
Thread importantThread = new Thread(
new Runnable() {
public void run () {
//do something
}
}
);
Thread currentThread = new Thread(
new Runnable() {
public void run () {
//do something
}
}
);
importantThread.start(); // Line 1
importantThread.join(); // Line 2
currentThread.start(); // Line 3
}
In the above example, main thread is executing. On Line 1, a new
thread called importantThread is ready to run. But at Line 2, main
thread joins the importantThread. Now it lets importantTread to
finish and then it moves to Line 3. So currentThread at Line 3 will
not start till the importantThread has finished