记一次Thread.join的思考(如何打断join阻塞状态?)

最近在温习Java Thread的各种状态变化,想到Thread.join方法的用法:当前线程阻塞到被join线程结束后才继续执行。于是我翻了以下Thread.join方法的源码,如下图:

在发现join方法内部其实使用wait方法来实现的,我立即想到了对应的notify/notifyAll方法,当时就想着既然是用wait进行线程休眠,我是不是可以用notify/notifyAll来进行唤醒?打破join引起的阻塞?

1、有想法就实践吧,第一次的测试代码:

public static void testJoin() {
        final Thread thread = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        System.out.println("InterruptedException from sleep");
                    }
                    System.out.println("thread running: " + i);
                }

            }
        };

        thread.start();

        (new Thread(() -> {
            try {
                Thread.sleep(1000 * 6);
                synchronized (thread) {
                    thread.notifyAll(); // wake wait status
                }
                System.out.println("notify join thread");
            } catch (InterruptedException e) {
                System.out.println(e.getClass().getName() + " is thrown 1");
            }
        }
        )).start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            System.out.println(e.getClass().getName() + " is thrown 2");
        }
        System.out.println("main is running after join!");
    }

运行结果如下:

thread running: 0
thread running: 1
thread running: 2
notify join thread
thread running: 3
thread running: 4
thread running: 5
thread running: 6
thread running: 7
thread running: 8
thread running: 9
main is running after join!

结果显示,main线程调用join的代码处并没有解除阻塞状态,于是我返回join源码处仔细查看,wait代码处被唤醒后代码会如何执行呢?这才发现wait被while循环包围,终止条件是被join的thread已经执行完成(isAlive()),好吧,这才是join方法的目的:被join线程执行完成后当前线程才能继续执行。这次实验加深了我对join方法的理解,同时也引发了另一个问题的思考:join的thread执行完成后,join方法中的wait状态是怎么被唤醒的呢?在JDK源码中找了好一会也没找到答案,后来在网上搜了下,有人说是JVM内部实现的,在thread执行完毕后进行的唤醒(c++代码),暂时记录一下,以后有机会研究JVM源码的时候再求证一下,参考链接:https://www.jianshu.com/p/fc51be7e5bc0

2、上面的第一次尝试并没有实现我的需求,后来我在看到Thread的interrupt相关知识点时,又想起来这个问题。以下是我的第二次尝试,最终结果是main线程跳出join阻塞状态继续运行了,也就是说join阻塞状态是可以被打断的,实现方式就是使用线程的interrupt方法。

public static void testJoin() {
        final Thread thread = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        System.out.println("interruptedException thrown by sleep!");
                    }
                    System.out.println("thread running: " + i);
                }

            }
        };

        thread.start();

        final Thread mainThread = Thread.currentThread();
        (new Thread(() -> {
            try {
                Thread.sleep(1000 * 10);
                mainThread.interrupt();
                System.out.println("notify join thread");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        )).start();


        try {
            thread.join();
        } catch (InterruptedException e) {
            System.out.println(e.getClass().getName() + " is thrown~");
        }
        System.out.println("main is running after join!");
    }

运行结果如下:

thread running: 0
thread running: 1
thread running: 2
thread running: 3
thread running: 4
notify join thread
java.lang.InterruptedException is thrown~
main is running after join!
thread running: 5
thread running: 6
thread running: 7
thread running: 8
thread running: 9

这里有一点需要说的是,需调用mainThread的interrupt方法,而不是被join线程的interrupt方法,因为执行join方法中的wait代码段的当前线程是main线程,被阻塞的也是main线程,被join线程在wait处只是作为竞争资源和锁监控对象。实际测试,如果调用被join线程的interrupt方法,只会打断被join线程run方法中的sleep(2000)的休眠状态。

总结一下,Thread的interrupt操作可以让线程从wait/sleep/join等休眠状态中恢复过来并抛出InterruptedException异常,附上interrupt方法的注释。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值