多线程,加入线程。

join();     当前线程暂停,等待指定的线程执行结束后,当前线程在继续。

join(int); 可以等待指定的毫秒之后继续。

public class Demo05 {
public static void main(String[] args) {
Thread t1 = new Thread(){
public void run(){
for(int i = 0; i < 10; i++){
System.out.println(getName()+"AAAAAAAAAAA");
}
}
};
Thread t2 = new Thread(){
public void run(){
for(int i = 0; i < 10; i++){
if(i == 2){
try {
                t1.join(); //T1这个线程插队执行完毕后,在执行T2.
t1.join(1); //T1这个线程插队指定时间,过了指定时间后两条线程交替执行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(getName()+"BBBBBBBBBBB");
}
}
};
t1.start();
t2.start();
}
}
原文地址:https://www.cnblogs.com/wangffeng293/p/13363046.html