java中的强制线程(插队)

线程插队就是说的是主线程执行过程中不让其先执行,让我们想执行的线程进行执行

线程插队用到的函数是Thread里面的Join()函数

代码如下:

package ThreadStudy.TestThread;
//线程强制//插队
public class TestJoin implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 200; 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();//如果主线程执行到200的时候会被停止,然后会执行自己创建的线程
            }
            System.out.println("main"+i);//主线程执行的代码
        }


    }
}