线程强制执行
线程强制执行 :Thread.join();
- Join合并线程,待此线程执行完成后,在执行其它线程,其他线程阻塞;
- 可以想象成插队
Code
public class JoinTest implements Runnable{
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println("VIP来喽"+i);
}
}
public static void main(String[] args) throws InterruptedException {
// 启动我们的线程
JoinTest joinTest = new JoinTest();
Thread thread = new Thread(joinTest);
thread.start();
for (int i = 1; i <= 1000; i++) { // 主线程
if (i == 200){
thread.join(); // 线程礼让
}
System.out.println("主线程"+i);
}
}
}
- 主线程200之前VIP线程全部执行完毕