一、源码:
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
public boolean isInterrupted() {
return isInterrupted(false);
}
private native boolean isInterrupted(boolean ClearInterrupted);
二、理解:
1.两者都调用的最后边这个方法,只是参数不同,参数ClearInterrupted=false表示无复位操作,ClearInterrupted=true表示有复位操作;
2.native关键字的意思是通知操作系统去实现复位的动作;
3.isInterrupted()只返回线程状态;interrupted()除了返回线程状态外还有复位的动作;
4.复位操作指的是把当前线程的中断标志位置为false;
三、执行代码:
public class InterruptTest {
public static void main(String[] args) {
System.out.println(Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().isInterrupted());
System.out.println(Thread.interrupted());
System.out.println(Thread.currentThread().isInterrupted());
System.out.println(Thread.interrupted());
System.out.println(Thread.currentThread().isInterrupted());
}
}
四、运行结果:
false
true
true
false
false
false