美文网首页
顺序打印t1、t2、t3三个线程

顺序打印t1、t2、t3三个线程

作者: flyjar | 来源:发表于2023-04-01 01:12 被阅读0次

//join是获取处理器独占权,不与其他线程并行,执行了此方法的线程将获取处理器独占权,等其执行完,其他线程才能继续执行

@Test
    public void test2() {


        Thread t1 = new Thread(() -> {

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
        });
        Thread t2 = new Thread(() -> {

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("222");
        });


        Thread t3 = new Thread(() ->
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("333");
        });

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();

        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t3.start();

        try {
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

相关文章

网友评论

      本文标题:顺序打印t1、t2、t3三个线程

      本文链接:https://www.haomeiwen.com/subject/khdkddtx.html