查查知识网

interrupt interrupt怎么用

发布者:高熙
导读线程的 interrupt方法只是设置线程的中断标记,当对处于阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出Interru

线程的 interrupt()方法只是设置线程的中断标记,当对处于阻塞状态的线程调用interrupt方法时(处于阻塞状态的线程是调用sleep, wait, join 的线程),会抛出InterruptException异常,而这个异常会清除中断标记。正常运行的线程调用interrupt()不会清除中断标记。

通过interrupt()我们可以优雅的结束一个线程;有人会说stop()也可以呀,但是stop会出现一种情况,如果线程锁住了共享资源,那么当它调用stop被杀死后就再也没有机会释放锁了,其它线程将永远无法获取,所有这种结束线程的方式是不安全的也是不建议的。

优雅的结束线程是指,当线程死亡的时候,它可以做一些善后的事情。我们通过如下的代码就可以优雅的结束一个线程。

package com.zd.demo;

public class Test01 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                while (true){
                    boolean b = Thread.currentThread().isInterrupted();
                    //如果为真,就结束线程
                    if (b){
                        System.out.println(&34;执行善后工作...&34;);
                        break;
                    }

                    try {
                        Thread.sleep(1000);
                        System.out.println(&34;线程执行业务功能&34;);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    }
                }
            }
        };
        Thread t1 = new Thread(runnable,&34;t1&34;);
        t1.start();
        try {
            Thread.sleep(4000);
            t1.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
&34;C:Program FilesJavajdk1.8.0_101binjava&34; -javaagent:D:BaiduNetdiskDownloadIDEAIDEA安装包ideaIU-2017.3.3.winlibidea_rt.jar=6134:D:BaiduNetdiskDownloadIDEAIDEA安装包ideaIU-2017.3.3.winbin -Dfile.encoding=UTF-8 -classpath &34;C:Program FilesJavajdk1.8.0_101jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_101jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_101jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_101jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_101jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_101jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_101jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_101jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_101jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_101jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_101jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_101jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_101jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_101jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_101jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_101jrelibjce.jar;C:Program FilesJavajdk1.8.0_101jrelibjfr.jar;C:Program FilesJavajdk1.8.0_101jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_101jrelibjsse.jar;C:Program FilesJavajdk1.8.0_101jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_101jrelibplugin.jar;C:Program FilesJavajdk1.8.0_101jrelibresources.jar;C:Program FilesJavajdk1.8.0_101jrelibrt.jar;C:UserszhaodongIdeaProjectsboot1demotargetclasses;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot-starter-web2.5.5spring-boot-starter-web-2.5.5.jar;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot-starter2.5.5spring-boot-starter-2.5.5.jar;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot2.5.5spring-boot-2.5.5.jar;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot-autoconfigure2.5.5spring-boot-autoconfigure-2.5.5.jar;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot-starter-logging2.5.5spring-boot-starter-logging-2.5.5.jar;C:Userszhaodong.m2repositorychqoslogbacklogback-classic1.2.6logback-classic-1.2.6.jar;C:Userszhaodong.m2repositorychqoslogbacklogback-core1.2.6logback-core-1.2.6.jar;C:Userszhaodong.m2repositoryorgapachelogginglog4jlog4j-to-slf4j2.14.1log4j-to-slf4j-2.14.1.jar;C:Userszhaodong.m2repositoryorgapachelogginglog4jlog4j-api2.14.1log4j-api-2.14.1.jar;C:Userszhaodong.m2repositoryorgslf4jjul-to-slf4j1.7.32jul-to-slf4j-1.7.32.jar;C:Userszhaodong.m2repositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;C:Userszhaodong.m2repositoryorgyamlsnakeyaml1.28snakeyaml-1.28.jar;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot-starter-json2.5.5spring-boot-starter-json-2.5.5.jar;C:Userszhaodong.m2repositorycomfasterxmljacksoncorejackson-databind2.12.5jackson-databind-2.12.5.jar;C:Userszhaodong.m2repositorycomfasterxmljacksoncorejackson-annotations2.12.5jackson-annotations-2.12.5.jar;C:Userszhaodong.m2repositorycomfasterxmljacksoncorejackson-core2.12.5jackson-core-2.12.5.jar;C:Userszhaodong.m2repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.12.5jackson-datatype-jdk8-2.12.5.jar;C:Userszhaodong.m2repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.12.5jackson-datatype-jsr310-2.12.5.jar;C:Userszhaodong.m2repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.12.5jackson-module-parameter-names-2.12.5.jar;C:Userszhaodong.m2repositoryorgspringframeworkbootspring-boot-starter-tomcat2.5.5spring-boot-starter-tomcat-2.5.5.jar;C:Userszhaodong.m2repositoryorgapachetomcatembedtomcat-embed-core9.0.53tomcat-embed-core-9.0.53.jar;C:Userszhaodong.m2repositoryorgapachetomcatembedtomcat-embed-el9.0.53tomcat-embed-el-9.0.53.jar;C:Userszhaodong.m2repositoryorgapachetomcatembedtomcat-embed-websocket9.0.53tomcat-embed-websocket-9.0.53.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-web5.3.10spring-web-5.3.10.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-beans5.3.10spring-beans-5.3.10.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-webmvc5.3.10spring-webmvc-5.3.10.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-aop5.3.10spring-aop-5.3.10.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-context5.3.10spring-context-5.3.10.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-expression5.3.10spring-expression-5.3.10.jar;C:Userszhaodong.m2repositoryorgslf4jslf4j-api1.7.32slf4j-api-1.7.32.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-core5.3.10spring-core-5.3.10.jar;C:Userszhaodong.m2repositoryorgspringframeworkspring-jcl5.3.10spring-jcl-5.3.10.jar&34; com.zd.demo.Test01
线程执行业务功能
线程执行业务功能
线程执行业务功能
java.lang.InterruptedException: sleep interrupted
执行善后工作...
	at java.lang.Thread.sleep(Native Method)
	at com.zd.demo.Test01$1.run(Test01.java:20)
	at java.lang.Thread.run(Thread.java:745)

Process finished with exit code 0