Interrupted Clears Flag IsInterrupted Does Not
Tell the two interrupt probes apart: static Thread.interrupted reads and clears the flag, instance isInterrupted only reads, and blocking calls clear it too.
Code
public class InterruptFlagProbe {
public static void main(String[] args) {
// 静态 Thread.interrupted() 读取并 clears 当前线程标志
Thread.currentThread().interrupt();
System.out.println("first interrupted(): " + Thread.interrupted());
System.out.println("second interrupted(): " + Thread.interrupted());
// 实例 isInterrupted() 只读不清:连续两次结果一致
Thread.currentThread().interrupt();
System.out.println("isInterrupted read 1: "
+ Thread.currentThread().isInterrupted());
System.out.println("isInterrupted read 2: "
+ Thread.currentThread().isInterrupted());
// 带标志进阻塞方法:sleep 立刻抛 InterruptedException 并清掉标志
try {
Thread.sleep(60_000);
} catch (InterruptedException e) {
System.out.println("sleep threw, flag now: " + Thread.interrupted());
}
}
}Inside the snippet
Interrupted Clears Flag IsInterrupted Does Not reduces to the class InterruptFlagProbe and the function main.
Watch out for
- There is no dependency tree here — the platform is the whole dependency.
Putting it to use
Declared as public static void main(String[] args)
The file announces its steps in the output: first interrupted():, second interrupted():, isInterrupted read 1:.
Core lines
Thread.currentThread().interrupt();
Thread.sleep(60_000);
Interactive Tools for This Topic
FAQ
- What does Interrupted Clears Flag IsInterrupted Does Not print when run?
- The script runs standalone; its 5 labeled outputs include `first interrupted():`, `second interrupted():`, `isInterrupted read 1:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.