关键字:join
官方文档解释,这里就不贴了。
- 合并线程,线程强制执行 join
- 类似于【生活中的:插队】
- 它执行完,其他再执行,阻塞其他线程,通常不建议使用
示例代码:
public class ThreadTest8 implements Runnable{
public static void main(String[] args) throws InterruptedException {
ThreadTest8 t=new ThreadTest8();
Thread thread=new Thread(t);
for (int i = 0; i < 100; i++) {
if (i==50) {
thread.start();
thread.join(); //阻塞了主线程
}
System.out.println("我是主线程————>"+i);
}
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("我是子线程,join之后执行的--->"+i);
}
}
}
执行结果: