JDK说明:
join
public final void join() throws InterruptedException
-
等待该线程终止。
-
-
抛出:
-
InterruptedException
- 如果任何线程中断了当前线程。当抛出该异常时,当前线程的 中断状态 被清除
-
测试代码:
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);
}
}
}
可以看出 等线程A 执行完之后 线程B才开始执行
非常清楚是不是呵呵