join():
作用是将当前线程插入到某一个线程中,使某一个线程进入阻塞状态。
当前线程执行完后,另一个线程进入就绪状态。
如图:将t1线程插入到t2线程中,使t2线程进入阻塞状态,当t1线程执行完之后,t2线程则进入就绪状态,等待被分配时间片段。
public class JoinThread {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
public void run() {
for(int i=0;i<100;i++) {
System.out.println("树");
}
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<100;i++) {
System.out.println("啄木鸟");
}
}
});
t1.start();
t2.start();
}
}