/**
* join()为让线程j1加入到线程j2的前面,在线程j1执行完之前,线程j2不能工作
*/
public class JoinTest extends Thread{
public JoinTest(String name){
super(name);
}
public static void main(String[] args) throws InterruptedException {
JoinTest j1 = new JoinTest("J1");
JoinTest j2 = new JoinTest("J2");
j1.start();
j1.join();//等待该线程结束,这里还可以传递参数为join加入的时间
j2.start();
System.out.println("over");
}
@Override
public void run(){
for(int i=1; i<=10; i++){
System.out.println(Thread.currentThread().getName()+"##"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 运行结果:
J1##1
J1##2
J1##3
J1##4
J1##5
J1##6
J1##7
J1##8
J1##9
J1##10
over
J2##1
J2##2
J2##3
J2##4
J2##5
J2##6
J2##7
J2##8
J2##9
J2##10
*/
}