并发系列之线程中断

2019-07-27  本文已影响0人  阿伦故事2019

曼彻斯特大学(英国)校训:“知识,睿智,人道。”


午夜时分,毫无睡意,要不聊聊线程中容易忽略的方面,即线程中断,这也是面试的重灾区。有时面试线程中断,很多人不知所云,甚至有些说没用过中断,我只能一脸懵逼说声好吧,然后你就可以出门左转。。。

一 概述线程中断

线程的中断是一种协作机制,当然自己也可以中断自己,但中断不意味着线程的终止;中断只是修改线程的一个标志位,当线程察觉到自身接收到中断的请求,可自行进行中断处理,是进行特定业务处理,线程终止,抛出中断异常等等,完全视业务情况而定。
在实际工作中,中断的使用场景主要有某个线程在等待特定条件到来而后继续执行,如sleep/wait/join等;如果特定条件提前到来,可以通过中断该线程来进行通知,以便做后续的处理。这是基于在当该线程是中断状态时,在执行声明InterruptedException的方法时,需要捕获或抛出InterruptedException。从这个方面思考,就能清晰地认识到中断是线程间的协作机制。

二 中断的检测与设置

Thread类提供两个方法进行中断状态检测,一个是静态的interrupted,另一个是普通方法isInterrupted,两者底层均是调用native isInterrupted(boolean ClearInterrupted)来实现,当ClearInterrupted为true时,此方法可以清除中断标志位的;interrupt()方法是设置线程的中断状态标志的,底层也是依赖native方法,具体看下演示demo:

/**
 * @author 阿伦故事
 * @Description:描述线程中断的检测与设置
 * */
@Slf4j
public class InterruptCheckAndSet {
    public static void main(String[] args) {
        InterruptCheckAndSet interruptCheckAndSet = new InterruptCheckAndSet();
        interruptCheckAndSet.check();
    }
    /**
     * 检测中断状态
     * isInterrupted:只是检测中断标记,并不会清除中断标记
     * interrupted:检测中断标记,并清除中断标记
     * interrupt:设置中断标志位
     * */
    public void check(){
        Thread current = Thread.currentThread();
        //false
        log.info("--thread name:"+current.getName() + ",isInterrupted 中断标志位:"+current.isInterrupted());
        //false
        log.info("--thread name:"+current.getName() + ",interrupted 中断标志位:"+current.interrupted());
        //执行中断
        current.interrupt();
        //true
        log.info("--thread name:"+current.getName() + ",isInterrupted 中断标志位:"+current.isInterrupted());
        //true
        log.info("--thread name:"+current.getName() + ",interrupted 中断标志位:"+current.interrupted());
        //false 被interrupted方法清除了中断标志
        log.info("--thread name:"+current.getName() + ",再次isInterrupted 中断标志位:"+current.isInterrupted());
    }
}

三 中断的处理与响应

中断的处理需要视业务场景而定,但无非三种处理,其一是直接抛出中断异常,其二是捕获异常做特定的业务处理后再抛出,其三是在其二的基础上不进行中断异常抛出,而是发起重新中断,这是因为某些方法(如run/call)不支持声明异常,若是只捕获处理,上层又无法感知,此时重新中断是个不错的选择,具体看下演示demo:

/**
 * @author 阿伦故事
 * @Description:描述线程中断的处理与响应
 * */
@Slf4j
public class InterruptResponse {

    public static void main(String[] args) {
        InterruptResponse interruptResponse = new InterruptResponse();
        //interruptResponse.throwRespon();
        //interruptResponse.catchRespon();
        interruptResponse.ReInterruptRespon();
    }
    /**
     * way1:直接抛出中断异常
     * */
    public void throwRespon() throws InterruptedException{
        Thread.currentThread().interrupt();
        Thread.sleep(2000);
    }
    /**
     * way2:捕获后重新抛出
     * */
    public void catchRespon() throws InterruptedException{
        Thread.currentThread().interrupt();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            log.info("--执行特定业务处理--");
            throw e;
        }
    }
    /**
     * way3:捕获后重新中断
     * */
    public void ReInterruptRespon() {
        Thread thread = new Thread(()->{
           log.info("--执行子任务--");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                log.info("--执行特定业务处理--");
                //重新中断
                Thread.currentThread().interrupt();
            }
        });
        thread.start();
        thread.interrupt();
        log.info("--exit--");
    }
}

特此声明:
分享文章有完整的知识架构图,将从以下几个方面系统展开:
1 基础(Linux/Spring boot/并发)
2 性能调优(jvm/tomcat/mysql)
3 高并发分布式
4 微服务体系
如果您觉得文章不错,请关注阿伦故事,您的支持是我坚持的莫大动力,在此受小弟一拜!


每篇福利:

评论区打出车型.jpg
上一篇 下一篇

猜你喜欢

热点阅读