首页 > 编程语言 >JAVA线程-interrupt

JAVA线程-interrupt

时间:2023-01-12 13:55:40浏览次数:48  
标签:JAVA Thread System println 线程 interrupt hello

JAVA线程-interrupt

中断一个线程非常简单,只需要在其他线程中对目标线程调用interrupt()方法,目标线程需要反复检测自身状态是否是interrupted状态,如果是,就立刻结束运行。

案例一:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new MyThread();
        t.start();
        Thread.sleep(1); // 暂停1毫秒
        t.interrupt(); // 中断t线程
        t.join(); // 等待t线程结束
        System.out.println("end");
    }
}

class MyThread extends Thread {
    public void run() {
        int n = 0;
        while (! isInterrupted()) {
            n ++;
            System.out.println(n + " hello!");
        }
    }
}

Output:

....
33 hello!
34 hello!
35 hello!
end

上述案例很简单,就是调用mythread的interrupt()方法,MyThread内部run中对线程中的中断状态进行判断,并没有真正意义上中断线程。

案例二:

public class ThreadInterruptTest {
    public static void main(String[] args) {
        try {
            Thread t = new MyThread();
            t.start();
            Thread.sleep(1); // 暂停1毫秒
            System.out.println("before invoke interrupted");
            t.interrupt(); // 中断t线程
            System.out.println("before main join");
            t.join(); // 等待t线程结束
            System.out.println("end");
        } catch (InterruptedException e) {
            System.out.println("main interrupted!");
        }
    }
}

class MyThread extends Thread {
    public void run() {
        int n = 0;
        try {
            while (!isInterrupted()) {
                n++;
                System.out.println(n + " hello!");
            }
            // mythread如果被调用了interrupt方法,并且mythread线程run方法中有抛出InterrutpedException的方法,就会抛出,没有就不会抛出
            Thread.sleep(1);
        } catch (InterruptedException e) {
            System.out.println("mythread interrupted");
        }
    }
}

Output:

1 hello!
.....
51 hello!
before invoke interrupted
before main join
52 hello!
mythread interrupted
end

该案例就是案例一中,对MyThread中run方法新增了Thread.sleep方法调用,并catch了InterruptedException

此时发现MyThread中抛出了InterruptedException,但是案例一中并没有抛出

所以对于处在sleep()方法阻塞下的线程,interrupt()方法会使线程抛出一个异常。

案例三:

public class ThreadInterruptTest2 {
    public static void main(String[] args) {
        try {
            Thread t = new MyThread2();
            t.start();
            Thread.sleep(1000);
            t.interrupt(); // 中断t线程
            System.out.println("before main join");
            t.join(); // 等待t线程结束
            System.out.println("end");
        } catch (InterruptedException e) {
            System.out.println("main interrupted!");
        }
    }
}

class MyThread2 extends Thread {
    public void run() {
        Thread hello = new HelloThread();
        hello.start(); // 启动hello线程
        try {
            hello.join(); // 等待hello线程结束
        } catch (InterruptedException e) {
            System.out.println("mythread interrupted!");
        }
        hello.interrupt();
    }
}

class HelloThread extends Thread {
    public void run() {
        int n = 0;
        while (!isInterrupted()) {
            n++;
            System.out.println(n + " hello!");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("hello interrupted");
                break;
            }
        }
    }
}

Output:

1 hello!
2 hello!
3 hello!
4 hello!
5 hello!
6 hello!
7 hello!
8 hello!
9 hello!
10 hello!
before main join
mythread interrupted!
hello interrupted
end

比案例二中更复杂了,嵌套线程调用

但是基本逻辑一样,就是在调用线程中断方法,线程的具体实现run中具有join调用,会抛出InterruptedException

所以对于处在sleep()、wait()和join()方法阻塞下的线程,interrupted方法会使线程抛出一个异常。

总结:

interrupt()不会去真正意义上的打断一个正在运行的线程,而是修改这个线程的中断状态码(interrupt status)。同时,对于处在sleep()、wait()和join()方法阻塞下的线程,该方法会使线程抛出一个异常。

与interrupt类似的方法

java.lang.Thread#isInterrupted()

判断目标线程是否被中断,不会清除中断标记。

java.lang.Thread#interrupted

判断目标线程是否被中断,会清除中断标记。

参考:

中断线程

Java多线程--正确理解interrupt()、interrupted()和isInterrupted()

标签:JAVA,Thread,System,println,线程,interrupt,hello
From: https://www.cnblogs.com/hongdada/p/17046446.html

相关文章

  • Java类、对象以及(静态)方法的探讨
    大家好,这是我的第一篇博客。在这里我想跟大家分享一下Java最基础的ClassandObject,也就是我们熟说的类和对象,以及对Java方法的探讨。初学时的我因为没有对这里......
  • 继承Thread类创建多线程
    之前我们学习了多线程,今天来说Java程序中如何实现多线程,Java提供了两种多线程实现方法,今天就来说说第一种继承Thread类创建多线程,另一种方法请看下篇!在学习多线程之前,先来看......
  • Java程序的运行机制
    使用Java语言进行程序设计时,不仅要了解Java语言的显著特点,还需要了解Java程序的运行机制。Java程序员运行时,必须经过编译和运行两个步骤。首先将后缀为.java的源文件进行编......
  • Java程序员必看之:eclipase中maven的配置
     需要准备:apache-maven-3.2.3.zip包  Repository.rar本地仓库                              ......
  • Jenkins部署Java应用到Tomcat服务器
    对于Java程序,Jenkins需要使用构建工具,如maven、ant等,其中maven比较流行。这里就用maven实现Java应用的部署。1GitLab准备Java代码2部署Tomcat并配置apt-get-yinstallto......
  • 【收藏】不可错过的javascript迷你库
    最近看着下自己的githubstar,把我吓坏了,手贱党,收藏癖的我都收藏了300+个仓库了,是时候整理一下了。Unix主张kiss,小而美被实践是最好用的,本文将介绍笔者收集的一些非常赞的......
  • java 程序打包成 exe安装包 兼容性强
    找了很多资料,网上的打包成exe用的最多的是exe4j。确实一段时间我也是用exe直到后来开发一个应用的时候出现了兼容性问题。于是再次研究,如果你是想要吧开发好java桌面应用......
  • 通过事件总线EventBus/AsyncEventBus进行JAVA模块解耦 (史上最全)
    文章很长,而且持续更新,建议收藏起来,慢慢读!疯狂创客圈总目录博客园版为您奉上珍贵的学习资源:免费赠送:《尼恩Java面试宝典》持续更新+史上最全+面试必备2000页+面......
  • Java的基础语法
    注释、标识符、关键字Java中的注释有三种单行注释多行注释文档注释publicclassHelloWorld{/*这是第一个Java程序*它将输出HelloWorld*这是一......
  • JavaScript的深拷贝实现
    在实际开发当中,我们经常会遇到要对对象进行深拷贝的情况。而且深拷贝这个问题在面试过程中也经常会遇到,下面就对本人在学习过程中的收获,做以简单的总结。什么是浅拷贝,什么是......