美文网首页
线程中断的相关方法

线程中断的相关方法

作者: 凉风拂面秋挽月 | 来源:发表于2020-03-14 16:09 被阅读0次

在Thread类中和线程中断的相关的方法有三个interrupt()、interrupted()和isInterrupted()。

interrupt

其作用是中断此线程(此线程不一定是当前线程,而是指调用该方法的Thread实例所代表的线程),但实际上只是给线程设置一个中断标志,线程仍会继续运行。
如果线程A因为调用了wait、join或sleep方法导致线程阻塞挂起的状态下,线程B调用了线程A的interrupt方法,线程A会在阻塞线程的地方抛出interrupt异常。

注意:线程抛出异常并不会引起整个线程的退出,如果线程异常后依然有逻辑代码未执行,则线程会继续执行未执行的代码,直到整个run方法执行结束。
在之前的jdk中提供过stop方法,但后来被取消了。也就是说,我们没有办法再线程未执行完之前,通过代码的形式杀死线程。
我写个demo看看吧。

public class myt implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("i="+(i+1));
             try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }       
    }
}
public class client {
    Thread thread;
    public void start() {
        myt myt = new myt();
        thread = new Thread(myt);
        thread.start();
    }
    public void disconnect() {
        thread.interrupt();
    }
    public static void main(String[] args) {
        client client = new client();
        client.start();
        client.disconnect();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(client.thread.isInterrupted());
        System.out.println("thread是否存活:"+client.thread.isAlive());

    }
}

子线程执行一个for方法打印0-9,每打印一次休息0.5秒。主线程启动子线程后,在3秒之后向子线程添加中断标志。然后来观察线程的执行情况。
执行结果

i=1
i=2
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at 线程.myt.run(myt.java:9)
    at java.lang.Thread.run(Unknown Source)
i=3
i=4
i=5
i=6
i=7
false
thread是否存活:true
i=8
i=9
i=10

interrupted()方法

作用是测试当前线程是否被中断(检查中断标志),返回一个boolean并清除中断状态,第二次再调用时中断状态已经被清除,将返回一个false。

isInterrupted()方法

作用是只测试此线程是否被中断 ,不清除中断状态。

interrupted可能带来的理解误差

我把上面的demo修改下,把for循环的sleep去掉。

public class myt implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("i="+(i+1));
           }        
    }
}
public class client {
    Thread thread;
    public void start() {
        myt myt = new myt();
        thread = new Thread(myt);
        thread.start();
    }
    public void disconnect() {
        thread.interrupt();
    }
    public static void main(String[] args) {
        client client = new client();
        client.start();
        client.disconnect();
        System.out.println(client.thread.isInterrupted());
                System.out.println(client.thread.interrupted());
                System.out.println(client.thread.isInterrupted());
    }
}

输出结果

true
i=1
i=2
i=3
i=4
i=5
false
true
i=6
i=7
i=8
i=9
i=10

因为当前线程指的是主线程,而不是你指定的子线程。主线程没有中断标志。
而且编译器也警告我们这样写的不对。因为interrupted是Thread类的静态方法。


image.png
image.png

优雅退出线程

既然不能用stop方法,且interrupt打断sleep之类无助于中断整个线程,那么就要靠这个标志来改写下代码的判断逻辑了。

public class myt implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("i="+(i+1));
            if(Thread.interrupted()) {
                break;
            }
        }       
    }
}
public class client {
    Thread thread;
    public void start() {
        myt myt = new myt();
        thread = new Thread(myt);
        thread.start();
    }
    public void disconnect() {
        thread.interrupt();
    }
    public static void main(String[] args) {
        client client = new client();
        client.start();
        client.disconnect();
        System.out.println(client.thread.isInterrupted());
    }
}
打印到1即跳出线程.png

interrupt标志是消耗品

interrupt标志被sleep、wait方法响应抛出异常后,标志会被清除。
所以如果上面的优雅退出demo改为在for循环中加入sleep,则结果不是我们想要的线程退出了。

public class myt implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("i="+(i+1));
                  try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(Thread.interrupted()) {
                break;
            }
        }       
    }
}
public class client {
    Thread thread;
    public void start() {
        myt myt = new myt();
        thread = new Thread(myt);
        thread.start();
    }
    public void disconnect() {
        thread.interrupt();
    }
    public static void main(String[] args) {
        client client = new client();
        client.start();
        client.disconnect();
        System.out.println(client.thread.isInterrupted());
    }
}

运行结果

true
i=1
java.lang.InterruptedException: sleep interrupted
i=2
    at java.lang.Thread.sleep(Native Method)
    at 线程.myt.run(myt.java:9)
    at java.lang.Thread.run(Unknown Source)
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10

原因在于标志被sleep响应后,中断标志被重置位false。

相关文章

  • JAVA并发编程(三)线程协作与共享

    1. 线程中断 java线程中断是协作式,而非抢占式 1.1. 线程中断相关方法 interrupt()将线程的中...

  • Java线程中断

    本文主要介绍Java线程中断一些相关的概念以及注意点 Java线程的中断并不是强制的中断,调用线程中断的方法时只是...

  • 线程中断的相关方法

    在Thread类中和线程中断的相关的方法有三个interrupt()、interrupted()和isInterr...

  • Java并发编程基础-理解中断

    章节 什么是中断 中断线程的方法 线程中断状态的判断以及何时被中断的线程所处 isInterrupted() 状态...

  • 8.3 线程控制

    线程中断 方法:void interrupt():中断线程(本质没有中断操作,只是给线程对象加了个标记,设置其中断...

  • 问题:什么是线程的中断标志,如何设置和检查它,它是如何和中断异常

    问题 什么是线程的中断标志,如何设置和检查它,它是如何和中断异常相关的 答案 线程的中断标志 线程的中断标志,是每...

  • 线程中断

    什么是线程中断?线程中断即线程运行过程中被其他线程打断了。 线程中断的重要方法2.1 java.lang.Thre...

  • java线程相关

    中断一个正在运行的线程 中断阻塞线程的方法 我们调用线程对象的interrupt()方法时会产生Interrupt...

  • 线程方法

    1.实例方法t1.interrupt()中断线程 线程中断并不会立即使线程退出,告知目标线程中断(设置中断标识位,...

  • JAVA基础-多线程和锁

    线程的基本流程image.png 如何中断一个线程方法一:调用interrupt方法,通知线程应该中断了:A.如果...

网友评论

      本文标题:线程中断的相关方法

      本文链接:https://www.haomeiwen.com/subject/onvishtx.html