中断机制之中断协商案例深度解析 下

说明

具体来说,当一个线程调用interrupt()时:

  • 如果线程处于正常活动状态,那么会将该线程的中断标志设置为true ,仅此而已。被设置中断标志的线程将继续正常运行,不受影响。所以,innerupt()并不能真正的中断线程,需要被调用的线程自己进行配合才行。
  • 如果线程处于被阻塞状态(例如处于sleep ,wait,join等状态) ,在别的线程中调用当前线程对象的interrupt方法,那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常。

案例

如果线程处于被阻塞状态(例如处于sleep ,wait,join等状态) ,在别的线程中调用当前线程对象的interrupt方法,那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常。

设置中断标识,线程读取到标识后自己停止当前线程

package com.kwfruit.thread.interruptdemo;

import java.util.concurrent.TimeUnit;

public class InterruptDemo3 {


    public static void main(String[] args) {
        //实例方法interrupt()仅仅是设置线程的中断状态位为true,不会停止线程

        Thread t1 = new Thread(()->{
            while (true){
                if(Thread.currentThread().isInterrupted()){
                    System.out.println(Thread.currentThread().getName()+"\t 中断标志为:"+Thread.currentThread().isInterrupted()+"程序停止");
                    break;
                }
                System.out.println("-----hello InterruptDemo3");
            }
        },"t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->t1.interrupt(),"t2").start();
    }

}


-----hello InterruptDemo3
-----hello InterruptDemo3
-----hello InterruptDemo3
-----hello InterruptDemo3
t1	 中断标志为:true程序停止

Process finished with exit code 0

设置中断标识后,让目标线程sleep 会抛异常

package com.kwfruit.thread.interruptdemo;

import java.util.concurrent.TimeUnit;

public class InterruptDemo3 {


    public static void main(String[] args) {
        //实例方法interrupt()仅仅是设置线程的中断状态位为true,不会停止线程

        Thread t1 = new Thread(()->{
            while (true){
                if(Thread.currentThread().isInterrupted()){
                    System.out.println(Thread.currentThread().getName()+"\t 中断标志为:"+Thread.currentThread().isInterrupted()+"程序停止");
                    break;
                }
                try {
                    Thread.sleep(200);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("-----hello InterruptDemo3");
            }
        },"t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->t1.interrupt(),"t2").start();
    }

}


小总结:所以说如上图所示,当调用interrupt()方法时,目标线程调用了sleep()方法 导致线程的中断状态被清除了 变成了false 而代码又在循环里面catch异常 不会打断当前循环,所以只有在catch块里面 在调一次interrupt()方法 在设置一下值 才会打断当前线程。

package com.kwfruit.thread.interruptdemo;

import java.util.concurrent.TimeUnit;

public class InterruptDemo3 {


    public static void main(String[] args) {
        //实例方法interrupt()仅仅是设置线程的中断状态位为true,不会停止线程

        Thread t1 = new Thread(()->{
            while (true){
                if(Thread.currentThread().isInterrupted()){
                    System.out.println(Thread.currentThread().getName()+"\t 中断标志为:"+Thread.currentThread().isInterrupted()+"程序停止");
                    break;
                }
                try {
                    Thread.sleep(200);
                }catch (InterruptedException e){
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }
                System.out.println("-----hello InterruptDemo3");
            }
        },"t1");
        t1.start();

        try {
            TimeUnit.MILLISECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->t1.interrupt(),"t2").start();
    }

}


总结

中断只是一种协商机制,修改中断标识位仅此而已,不是立刻stop打断

posted @ 2024-01-21 17:28  KwFruit  阅读(3)  评论(0编辑  收藏  举报