JDK description:
join
public final void join()
throws InterruptedException and waits for the thread to terminate.
Throws:
InterruptedException - if any thread interrupts the current thread. When this exception is thrown, the interrupt status of the current thread is cleared. Test code:
public static void main(String[] args) throws InterruptedException {
A a=new A();
B b=new B();
a.start();
a.join();
b.start();
}
}
class A extends Thread{
public void run(){
for(int i=0;i<10000;i++){
System.out.print("A "+i);
}
}
}
class B extends Thread{
public void run(){
for(int i=0;i<10000;i++){
System.out.print("B "+i);
}
}
}
It can be seen that thread B does not start executing until thread A finishes executing.
Very clear, right? Haha