线程中断的方法以及静态方法isInterrupted和实例方法interrupted的区别

线程中断

常见的有以下两种方式:

  1. 通过共享的标记来进行沟通
  2. 调用 interrupt() 方法来通知

通过共享的标记来实现中断
就是创建一个boolean类型的变量来控制循环是否进行,就是一个标记。
代码如下:

/**
 * 描述:标记法中断线程
 */
public class ThreadDemo {
    private static class MyRunnable implements Runnable {
        public volatile boolean isQuit = false;

        @Override
        public void run() {
            while (!isQuit) {
                //当标记为true时就中断了线程
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "1");
        thread.start();
        Thread.sleep(10 * 1000);
        target.isQuit = true;//让标记成为true
    }
}

这个也是标记法实现的调用thread.interrupt()和Thread.interrupted()方法返回的是Boolean类型的数据,

/**
 * 描述:调用thread.interrupt()和Thread.interrupted()方法
 */
public class Thread2 {
    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            // 两种方法均可以
            while (!Thread.interrupted()) {
                // Thread.interrupted()返回true时就中断了线程
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }

        }

        public static void main(String[] args) throws InterruptedException {
            MyRunnable target = new MyRunnable();
            Thread thread = new Thread(target, "1");
            thread.start();
            Thread.sleep(10 * 1000);
            thread.interrupt();//调用thread.interrupt()也可以中断返回true
        }
    }
}

调用 interrupt() 方法来通知

这种方式通知收到的更及时,即使线程正在 sleep 也可以马上收到
  1. 通过 thread 对象调用 interrupt() 方法通知该线程停止运行

  2. thread 收到通知的方式有两种:
    1). 如果线程调用了 wait/join/sleep 等方法而阻塞挂起,则以 InterruptedException 异常的形式通知,并清除中断标志
    2). 只是内部的一个中断标志被设置,thread 可以通过

    (1). Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志。
    (2). Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志。
    

代码如下:
1,调用sleep方法产生异常,从而中断线程

/**
 * 描述:调用sleep方法产生异常,从而中断线程
 */
public class ThreadInterrupt {
    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("通过异常收到了中断情况");
            }
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().isInterrupted());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "1");
        thread.start();
        thread.interrupt();
    }
}

2,(1),调用Thread.interrupted()

/**
 * 描述:调用了Thread.interrupted()会标志位清除
 */
public class ThreadDe {
    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.interrupted());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "1");
        thread.start();
        thread.interrupt();
    }
}

调用了Thread.interrupted()会标志位清除
在这里插入图片描述2,调用Thread.currentThread().isInterrupted()

/**
 * 描述:调用Thread.currentThread().isInterrupted()标志位不会清除
 */
public class ThreadDem {
    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().isInterrupted());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "1");
        thread.start();
        thread.interrupt();
    }
}

调用Thread.currentThread().isInterrupted()标志位不会清除
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值