【面经】interrupt()、interrupted()和isInterrupted()的区别与使用

       📝个人主页:五敷有你      

 🔥系列专栏:面经

⛺️稳中求进,晒太阳

interrupt方法

        如果打断线程正在sleep,wait,join会导致被打断的线程抛出InterruptedException,并清除打断标记。如果打断正在运行的线程,则会设置打断标记。park的线程被打断也会被设置打断标记

Interrupted方法

Thread类的静态方法。检测当前线程的中断标记,返回一个boolean并清除中断状态,其连续两次调用的返回结果不一样,因为第二次调用的时候,线程的中断状态已经被清除了,会返回一个false.

isInterrupted()方法

测试线程是否被打断,不会清除中断状态。

演示代码

场景:在sleep时打断

package jvm;


import lombok.extern.slf4j.Slf4j;

import java.util.logging.Logger;


public class TestInterrupt {



    public static void main(String[] args) throws InterruptedException {
        MyThread t1=new MyThread();
        t1.start();
        Thread.sleep(10000);
        t1.interrupt();
    }


}
class MyThread extends Thread{
    
    @Override
    public void run() {
        while (!this.isInterrupted()){
            try {
                System.out.println("执行1s");
                Thread.sleep(1000);

            } catch (InterruptedException e) {
                System.out.println("在睡觉的时候被打断了");
                throw new RuntimeException(e);
            }
        }
    }
}

如上:导致被打断的线程抛出InterruptedException,并清除打断标记。

场景:在正常运行时,打断标记的情况

package jvm;


import lombok.extern.slf4j.Slf4j;

import java.util.logging.Logger;


public class TestInterrupt {



    public static void main(String[] args) throws InterruptedException {
        MyThread t1=new MyThread();
        t1.start();
        Thread.sleep(10000);
        t1.interrupt();
    }


}
class MyThread extends Thread {

    @Override
    public void run() {
        while (!this.isInterrupted()) {

            System.out.println("执行1s");
            System.out.println("标记" + this.isInterrupted());


        }
    }
}

如上:正常运行时被打断是不会被清除标记的。

场景:使用park暂停的线程 打断标记情况

package jvm;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;
import java.util.logging.Logger;


public class TestInterrupt {



    public static void main(String[] args) throws InterruptedException {
        MyThread t1=new MyThread();
        t1.start();
        Thread.sleep(10000);
        t1.interrupt();
    }


}
class MyThread extends Thread {

    @Override
    public void run() {
        while (!this.isInterrupted()) {

            LockSupport.park();
            System.out.println("标记" + this.isInterrupted());


        }
    }
}

如上:park被暂停的线程被打断是不会清除标记的。

场景:测试Thread.interrupted静态方法

package jvm;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;
import java.util.logging.Logger;


public class TestInterrupt {



    public static void main(String[] args) throws InterruptedException {
        MyThread t1=new MyThread();
        t1.start();
        Thread.sleep(10000);
        t1.interrupt();
    }


}
class MyThread extends Thread {

    @Override
    public void run() {
      while (true){
          boolean interrupted = Thread.interrupted();

          System.out.println("标记:"+interrupted);
          if(interrupted){
              break;
          }
      }

    }
}

  • 83
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 62
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五敷有你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值