Why does the InterruptedException exception sometimes work, because Thread.interrupted () checks the interrupt flag and immediately resets it?
public class TestClass { public static void main(String[] args) throws InterruptedException { Thread threadExample = new Thread(new JoinClass()); threadExample.start(); Thread.sleep(1000); threadExample.interrupt(); } } class JoinClass implements Runnable { @Override public void run() { System.out.println("run.." + Thread.currentThread().isInterrupted()); while (!Thread.interrupted()); System.out.println("end while.." + Thread.currentThread().isInterrupted()); try { Thread.sleep(100); System.out.println("try.."); } catch (InterruptedException e) { System.out.println("catch.."); } System.out.println("end run.." + Thread.currentThread().isInterrupted()); } } run..false end while..false catch..false end run..false
threadExample.interrupt();? - Sergeycatch..falseoutput ofcatch..falseexactly from this example? Where doesfalsecome from ifSystem.out.println("catch..");? - SergeyInterruptedException(). Apparently it is not slightly edited . - Yuri Heiko