在现实生活中,有很多人,存在很多人插队,例如:下课去食堂吃饭,别人都在排队,打饭的阿姨你是家亲戚,你到窗口,阿姨就给你打了慢慢的饭菜,让别人羡慕,
在程序中也有这种情况出现,A正在执行,B要插队,A就得停下来,等B执行结束在去执行A
具体看如下代码:
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程vip来了-->"+i);
}
}
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 == 200){
thread.join();//插队
}
System.out.println("mian方法-->:"+i);
}
}
}