先看代码
package state;
//join
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("testJoin 线程"+i);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
for (int i = 0; i < 500; i++) {
if(i == 100){
thread.join();
}
System.out.println("main 线程"+i);
}
}
}
join合并线程,在main线程使用join后,main线程会阻塞,只有该线程执行完毕后才运行