首页 > 文章列表 > java isInterrupted()如何判断线程

java isInterrupted()如何判断线程

java isInterrupted
189 2022-08-06

说明

1、isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变2、调用interrupt()的时候会设置内部的一个叫interrupt flag的标识)。

实例

public static void main(String[] args) throws InterruptedException{
    Thread thread = new Thread(()->{
        while (true){}
    });
    thread.start();
    TimeUnit.SECONDS.sleep(1);
    System.out.println("Thread is interrupted :"+thread.isInterrupted());
    thread.interrupt();
    System.out.println("Thread is interrupted :"+thread.isInterrupted());
}

本教程操作环境:windows7系统、java10版,DELL G3电脑。