Thread的join方法[通俗易懂]

Thread的join方法[通俗易懂]Thread中的join方法主要的作用是让jion的线程加入当前线程,等加入的线程执行完之后才会执行当前线程。接下来看个例子:publicclassTestJoin{publicstaticvoidmain(String[]args)throwsInterruptedException{Threadt1=newThread(()->{try{Thr

大家好,又见面了,我是你们的朋友全栈君。

Thread中的join方法主要的作用是让jion的线程加入当前线程,等加入的线程执行完之后才会执行当前线程。

接下来看个例子:

public class TestJoin {

    public static void main(String[] args) throws InterruptedException {



        Thread t1 = new Thread(() ->
        {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this is t1");
        });

        Thread t2 = new Thread(() -> {

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


        Thread t3 = new Thread(() -> {
            System.out.println("this is t3");
        });

        t1.start();
        t2.start();
        t3.start();
        t1.join();
        t2.join();
        t3.join();
        System.out.println("运行结束");
    }

}

输出结果为:

this is t3
this is t1
this is t2
运行结束

可以看到输出是线程t1,t2,t3输出之后,才会输出到主线程。所以可以看出来join方法是把其他线程加入当前线程,等加入线程执行完之后才会执行当前线程。

如果想要t1,t2,t3再顺序输出,只需要在每个start方法后面再调用join就行了。这就就保证了每次主线程运行完一个线程,再去运行另外一个线程。

修改上面代码为:

public class TestJoin {

    public static void main(String[] args) throws InterruptedException {


        Thread t1 = new Thread(() ->
        {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("this is t1");
        });

        Thread t2 = new Thread(() -> {

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


        Thread t3 = new Thread(() -> {
            System.out.println("this is t3");
        });

        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();
        System.out.println("运行结束");
    }

}

输出为:

this is t1
this is t2
this is t3
运行结束

 

接下来我们再从源码分析下Thread的join方法是怎么实现的:

    public final void join() throws InterruptedException {
        join(0);
    }


    public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

 

由源码可以看到,其实底层就是wait方法,并且有一个while死循环,直到isAlive是False,也就是线程执行完成或者不存活。

顺带一提下Thread的yield的方法,此方法其实是只当前线程让出CPU回到就绪状态,线程再重新争抢CPU,但是结果可能是其他线程抢到CPU,也可能是自己抢到CPU。

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136450.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号