线程强制停止
- 采用Thread.join()方法
- Join合并线程,待此线程执行完后,再执行其他线程,其它线程阻塞,可以想象为插队
package MultiProcess;
//测试join方法
public class TestJoin {
public static void main(String[] args) throws InterruptedException {
MyJoin myJoin = new MyJoin();
Thread thread = new Thread(myJoin);
thread.start();
//主线程
for(int i = 0; i < 1000; i++){
if(i == 200){
thread.join();
}
System.out.println("main"+i);
}
}
}
class MyJoin implements Runnable{
@Override
public void run() {
for(int i = 0; i < 100; i++){
System.out.println("线程vip来了"+i);
}
}
}
结果
main196
main197
main198
main199
线程vip来了0
线程vip来了1
线程vip来了2