【JAVA】06使用interrupt()方法中断线程

位于Thread类中的interrupt()方法,它的作用就是中断线程。

一,停止正在运行的线程

public class MyThread implements Runnable {
    
    @Override
    public void run() {
       while (true){
           System.out.println("正在运行中");
       }
    }

}

    

    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread);
        thread.start();
        // 为了使效果更明显,使当前线程休眠一秒
        Thread.sleep(1000);
        // 中断线程
        thread.interrupt();
    }

从运行结果可与看到,控制台一直在无限打印“正在运行中”,线程并没有停下来,似乎并没有达到我们的预期效果。

原因在于,interrupt()方法仅仅只是将线程标记为中断状态,并没有实际去停止这个线程,如果想要线程停下来,则需要手动判断线程是否被中断,然后在做出反应。

判断线程是否被中断有两个方法,分别是isInterrupted()和interrupted(),isInterrupted()是一个非静态方法,它的作用是判断线程是否被中断,interrupted()是一个静态方法,它可以直接被Thread调用,作用也是判断线程是否被中断,并且,它可以去清楚中断标记,这也是interrupted()方法和isInterrupted()方法的区别。

  • isInterrupted()

isInterrupted()方法返回一个boolean类型,线程被中断返回true,否则false。

    @Override
    public void run() {
       while (true){
           // 获取当前执行的线程
           Thread thread = Thread.currentThread();
           // 判断线程是否被中断
           if(thread.isInterrupted()){
               break;
           }
           System.out.println("正在运行中");
       }
    }




    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread);
        thread.start();
        // 为了使效果更明显,使当前线程休眠一秒
        Thread.sleep(1000);
        // 中断线程
        thread.interrupt();
    }

从运行结果中可以看到,当线程执行一秒后线程被中断了。

  • interrupted()

interrupted()方法返回的也是一个boolean类型,线程被中断返回true,否则false。

    @Override
    public void run() {
       while (true){
           // 判断线程是否被中断
           if(Thread.interrupted()){
               break;
           }
           System.out.println("正在运行中");
       }
    }


    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread);
        thread.start();
        // 为了使效果更明显,使当前线程休眠一秒
        Thread.sleep(1000);
        // 中断线程
        thread.interrupt();
    }

从运行结果可以看到,当线程执行一秒后线程被中断了。

查看线程中断的标记

  • isInterrupted()
    @Override
    public void run() {
        while (true) {
            Thread thread = Thread.currentThread();
            System.out.println(thread.isInterrupted());
        }
    }


    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread);
        thread.start();
        // 为了使效果更明显,使当前线程休眠一秒
        Thread.sleep(1000);
        // 中断线程
        thread.interrupt();
    }

从运行结果可以看到,线程一开始并没有被中断,输出结果是false,一秒后,线程被中断,输出为true。

  • interrupted()
    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.interrupted());
        }
    }

    public static void main(String[] args) throws InterruptedException {

        MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread);
        thread.start();
        // 为了使效果更明显,使当前线程休眠一秒
        Thread.sleep(1000);
        // 中断线程
        thread.interrupt();
    }

从运行结果来看,和之前的isInterrupted()方法有不同,线程一开始并没有被中断,输出的结果为false,一秒后线程被中断,结果变为了true,然后线程中断标记被清除,输出的结果又变回了false。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值