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

       📝个人主页:五敷有你      

 🔥系列专栏:面经

⛺️稳中求进,晒太阳

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

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);
            }
        }
    }
}

【面经】interrupt()、interrupted()和isInterrupted()的区别与使用-LMLPHP如上:导致被打断的线程抛出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());


        }
    }
}

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

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

场景:使用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;
          }
      }

    }
}

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

04-06 15:33