java Thread interrupt方法

本文详细解析了Java中线程中断(interrupt)机制的工作原理,包括如何通过interrupt方法打断正在执行的线程,以及如何使用Thread类的静态方法interrupted()和实例方法isInterrupted()来检查线程的中断状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、interrupt用于打断【正在执行】的线程:interrupt方法有两个作用,一个是将线程的中断状态置位(中断状态由false变成true);另一个则是:让被中断的线程抛出InterruptedException异常
(1)如果当前运行线程处于sleep或wait状态,interrupt会打断此状态,并抛出InterruptedException异常
(2)如果当前运行线程没有处于sleep或wait状态,该方法只是将线程的interruptedStatus=true,
此时可以通过两种方式获取线程中断状态:
Thread类静态方法:Thread.interrupted(),如果该线程被打断,方法返回true,同时将interruptStatus还原(即设置为false),因此连续调用两次,第二次返回false
测试代码

public class TestInterruptThread {

    public static void main(String[] args) {
        InterruptThread interruptThread = new InterruptThread();
        interruptThread.start();

        interruptThread.interrupt();
        System.out.println("done");
    }
}


class InterruptThread extends Thread {
    @Override
    public void run(){
        for(int i=0;i<3;i++){
            if(Thread.interrupted()){
                System.out.println("front....");
            } else {
                System.out.println("end.....");
            }
        }

        System.out.println("thread continue working.....");
    }
}

测试结果

done
front....
end.....
end.....
thread continue working.....
Thread类实例方法:this.isInterrupted(),如果该线程被打断,该方法始终返回true
测试代码
    public class TestInterruptThread {

    public static void main(String[] args) {
        InterruptThread interruptThread = new InterruptThread();
        interruptThread.start();

        interruptThread.interrupt();
        System.out.println("done");
    }
}


class InterruptThread extends Thread {
    @Override
    public void run(){
        for(int i=0;i<3;i++){
            if(this.isInterrupted()){
                System.out.println("front....");
            } else {
                System.out.println("end.....");
            }
        }

        System.out.println("thread continue working.....");
    }
}
测试结果
    done
front....
front....
front....
thread continue working.....

实际上,检测到线程被中断后应该立即停止任务执行

class InterruptThread extends Thread {
    @Override
    public void run(){
          for(int i=0;i<3;i++){
                if(this.isInterrupted()){
                    break;   // 检测到线程被中断应该停止执行业务逻辑
                } else {
                    System.out.println("end.....");
                }
            }
        System.out.println("thread continue working.....");   // 仍然会执行
    }
}

线程被中断后,并没有立即停止运行,如果想要立即停止,可以抛出异常

class InterruptThread extends Thread {
    @Override
    public void run(){
          for(int i=0;i<3;i++){
                if(this.isInterrupted()){
                    throw new RuntimeException("中断后停止当前线程");
                } else {
                    System.out.println("end.....");
                }
            }
        System.out.println("thread continue working.....");   // 不会执行了
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值