Java join thread control usage
- 2020-04-01 01:34:59
- OfStack
The JDK description:
The join
Public final void the join ()
Throws InterruptedException waits for the thread to terminate.
Throws:
InterruptedException - if any thread interrupts the current thread. When the exception is thrown, the interrupted state of the current thread is cleared
Test code:
public class MyThread extends Thread {
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);
}
}
}
You can see that thread B doesn't start executing until thread A has finished executing
Very clear is not ha ha