前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Thread的join方法[通俗易懂]

Thread的join方法[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-07 14:29:34
5790
发布2022-09-07 14:29:34
举报
文章被收录于专栏:全栈程序员必看

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

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

接下来看个例子:

代码语言:javascript
复制
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("运行结束");
    }

}

输出结果为:

代码语言:javascript
复制
this is t3
this is t1
this is t2
运行结束

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

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

修改上面代码为:

代码语言:javascript
复制
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("运行结束");
    }

}

输出为:

代码语言:javascript
复制
this is t1
this is t2
this is t3
运行结束

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

代码语言:javascript
复制
    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。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档