【面试:并发篇23:多线程:join】join再理解

【面试:并发篇23:多线程:join】join再理解

00.前言

如果有任何问题请指出,感谢。

01.介绍

昨天我突然想到一个问题 join方法的底层实现是wait,执行wait的线程等待,那么它是怎么唤醒的呢?抱着这个问题我找了找博客最后发现了这篇文章https://blog.csdn.net/x541211190/article/details/109322537,这里面介绍了notifyAll的解锁时机,我发现了这么一段话在java中,Thread类线程执行完run()方法后,一定会自动执行notifyAll()方法 这句话如同醍醐灌顶,我立刻就理解了join方法里的wait是如何唤醒的了。
接下来我来模拟一下join的过程。

02.模拟

join源码


我们关注两个地方,第一是while循环条件 isAlive()它的作用是 判断调用join的线程是否存活 如果存活则执行wait()这里wait作用的线程是执行join的线程,注意这里 调用和执行的区别。举一个例子:我们在t2线程中执行了t1.join(),那么isAlive()判断的是t1线程是否存活,wait是让t2线程陷入等待

join例子

@Slf4j(topic = "c.TestInterJoin")
public class TestInterJoin {
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            for (int i=0;i<5;i++){
                log.debug("t1");
            }
        },"t1");

        Thread t2 = new Thread(()->{
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i=0;i<5;i++){
                log.debug("t2");
            }
        },"t2");
        t2.start();
        t1.start();

    }
}

结果

15:08:50.125 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t1] - t1
15:08:50.127 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2
15:08:50.128 c.TestInterJoin [t2] - t2

解释
我们创建了两个线程 t1 t2,我们在t2线程中执行t1.join使得t1线程同步到t2线程,结果也能证明这个结论

用wait模拟join例子

@Slf4j(topic = "c.TestInterJoinMN")
public class TestInterJoinMN {
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            Sleeper.sleep(2);
            for (int i=0;i<5;i++){
                log.debug("t1");
            }
        },"t1");

        Thread t2 = new Thread(()->{
            synchronized (t1){
                try {
                    t1.wait();
                } catch (InterruptedException e) {
                    e.pri(ntStackTrace();
                }
            }
            for (int i=0;i<5;i++){
                log.debug("t2");
            }
        },"t2");
        t2.start();
        t1.start();
    }
}

结果

15:12:16.361 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t1] - t1
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2
15:12:16.363 c.TestInterJoinMN [t2] - t2

解释
我们发现我们在t2线程创建了一个以 t1线程对象为监视器的锁,并执行了wait()方法导致t2线程陷入等待,此时如果我们没有notify或notifyAll方法的话 是不能唤醒t2线程的,但这里神奇的事情出现了 我们发现最终t2线程还是执行了。原因还是这句话在java中,Thread类线程执行完run()方法后,一定会自动执行notifyAll()方法,因为我们是把t1线程当成锁对象,但随着t1线程的运行结束 即run方法运行结束 t1线程执行了t1.notifyAll(),所以此时唤醒了 所有以t1为锁的线程,所以t2线程被唤醒了。这就是join的实现原理。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I cream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值