线程强制执行-join

线程强制执行-join

强制执行就是插队

join合并线程,待次线程执行完成后,再执行其他线程,其他线程阻塞

代码

package com.example.multi_thread;

public class TestJoin implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("I'm 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 < 10; i++) {
            System.out.println(i);
            if (i == 5) {
                thread.join();
            }
        }
    }

}
posted @ 2021-11-11 17:03  Oh,mydream!  阅读(28)  评论(0编辑  收藏  举报