线程篇——线程的停止与中断

interrupt

还是先看下Thread类中interrupt方法的注释:

Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.
If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
Throws:
SecurityExceptionif the current thread cannot modify this thread

​ 1.当这个线程在阻塞状态种被中断,那么中断状态会被清除,并且会收到InterruptedException异常

​ 2.如果在I/O操作中被中断,那么管道会立刻关闭,同时也会设置线程的中断状态,并且线程会收到ClosedByInterruptException异常

​ 3.如果线程在nio的选择器中被中断,那么会设置中断状态,然后立刻从选择操作中返回

​ 4.如果不是以上的状态,那么线程会被设置中断状态

​ 5.如果中断的不是一个活动的线程,那么不会有任何的影响

相关方法
interrupted

​ 查看当前线程是否已经中断

isInterrupted

​ 查看线程是否已经中断

那么这两个有什么区别呢?我们先看下源码:

public static boolean interrupted() {
   
        return currentThread().isInterrupted(true);
    }
public boolean isInterrupted() {
   
        return isInterrupted(false);
    }

interrupted的对象是当前线程,并且会清除中断状态

isInterrupted的对象是线程Thread对象,并且不会清除中断状态

interrupted与isInterrupted测试
测试一

​ 查看interrupted与isInterrupted的区别(中断线程测试)

public class InterruptTest implements Runnable {
   
    public static void main(String[] args) {
   
        Thread thread = new Thread(new InterruptTest());
        thread.start();
        thread.interrupt();
        System.out.println("interrupted:"+thread.interrupted());
        System.out.println("isInterrupted:"+thread.isInterrupted());
    }

    @Override
    public void run() {
   
        //给子线程找点事儿干,让它不要那么快结束
        for (int i = 0; i < 100000; i++) {
   
            System.out.println(i);
        }
    }
}

测试结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NrB9maok-1636863810902)(image-20211112205738947.png)]

测试结果分析

​ 说明两个方法的中断线程对象是不一样的,根据阅读源码的猜测,interrupted对象是当前线程,也就是创建Thread对象的线程;isInterrupted的对象是线程Thread对象,接下来继续测试证明这一点

测试二

​ 查看interrupted与isInterrupted的区别(中断线程测试)

public class InterruptTest2 implements Runnable {
   
    public static void main(String[] args) {
   
        Thread thread = new Thread(new InterruptTest2());
        thread.start();
        Thread.currentThread().interrupt();
        thread.interrupt();
        System.out.println("interrupted:"+thread.interrupted());
        System.out.println("isInterrupted:"+thread.isInterrupted());
    }

    @Override
    public void run() {
   
        //给子线程找点事儿干,让它不要那么快结束
        for (int i = 0; i < 100000; i++) {
   
            System.out.println(i);
        }
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值