中断机制

概念

提到java在中断机制,相信大家很容易想到Thread.interrupt,但是此举仅仅只是设置了中断标识。
java还提供了interrupted,isInterrupted 等方法联合使用作为中断线程的手段
1.boolean isInterrupted() 检查this线程是否设置了中断标志
2.static boolean interrupted()检测当前线程是否被中断了,并清除中断标注(也就是返回之前的中断状态,然后重置标志)
3.void interrupt() 中断this 线程

案例

public class Interrupt implements Runnable{

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Interrupt());
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2000L);
        thread.interrupt();
        System.out.println("thread end");
    }

    @Override
    public void run() {
        while (true){
            System.out.println(Thread.currentThread().isInterrupted());
            if(Thread.currentThread().isInterrupted()){
                System.out.println("i am interrupt");
            }else{
                System.out.println("i am not interrupt");
            }
        }
    }
}
执行结果
![image.png](https://upload-images.jianshu.io/upload_images/17398468-7e53f9bd0cd37995.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
可以看出程序不会结束 会根据当前的中断标识输出不同的信息

中断处理1:
中断标识为true就return

public class Interrupt implements Runnable{

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Interrupt());
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2000L);
        thread.interrupt();
        System.out.println("thread end");
    }

    @Override
    public void run() {
        while (true){
            System.out.println(Thread.currentThread().isInterrupted());
            if(Thread.currentThread().isInterrupted()){
                System.out.println("i am interrupt");
                return;  //直接退出
            }else{
                System.out.println("i am not interrupt");
            }
        }
    }
}
![image.png](https://upload-images.jianshu.io/upload_images/17398468-562930a1a1672f70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

中断处理1:
添加开关处理

public class Interrupt implements Runnable{

    public static boolean close = false;
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Interrupt());
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2000L);
       close = true;
        System.out.println("thread end");
    }

    @Override
    public void run() {
        while (!close){
           try {
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().isInterrupted());
            if(Thread.currentThread().isInterrupted()){
                System.out.println("i am interrupt");
                return;
            }else{
                System.out.println("i am not interrupt");
            }
        }
    }
}
执行结果
![image.png](https://upload-images.jianshu.io/upload_images/17398468-b3644278874c2e31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
同样可以是程序结束,但是遇到阻塞就不好处理了

中断处理3:
interrupt和开关一起使用

public class Interrupt implements Runnable{

    public static boolean close = false;
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Interrupt());
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2000L);
         close = true;
        thread.interrupt();
        System.out.println("thread end");
    }

    @Override
    public void run() {
        while (!close){
            try {
                Thread.sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("thread catch exception");
            }
            System.out.println(Thread.currentThread().isInterrupted());
            if(Thread.currentThread().isInterrupted()){
                System.out.println("i am interrupt");
                return;
            }else{
                System.out.println("i am not interrupt");
            }
        }
    }
}
执行结果
(https://upload-images.jianshu.io/upload_images/17398468-f44944a27b5e9b48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。