线程强制执行-Join
- Join合并线程,待此线程执行完后,再执行其他线程,其他线程阻塞
- 可以想象成插队。
案例
package Thread;
//测试Join方法
//想象为插队
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("我是VIP我先执行"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动我们的线程
TestJoin tj=new TestJoin();
Thread thread=new Thread(tj);
thread.start();
for (int i = 0; i < 500; i++) {
if (i == 100){
thread.join(); //执行run方法 Vip 插队
}
System.out.println("main"+i);
}
}
}