Test code:

public class Test { public static void main(String[] args) { System.out.println("Start T1"); Thread t = new T1(); t.start(); try { Thread.sleep(5000); } catch (InterruptedException ie) { } t.interrupt(); System.out.println("Of T1"); } public static class T1 extends Thread { @Override public void run() { try { Thread.sleep(10000); } catch (InterruptedException ie) { System.out.println(this.isInterrupted()); System.out.println("T1"); } } } } 

Conclusion

  Start T1 Of T1 false -> почему так ? T1 

    1 answer 1

    The sleep() method is already interrupted by itself. If the sleep() method is called when the interrupt state is set (using interrupt() ), the thread of execution does not go into a wait state. Instead, it clears its state (!) And throws an exception of type InterruptedException. Therefore, all you have to do is set the interrupt flag again with your hands. In your example, this will not be very obvious, but this is a general principle with respect to methods like sleep() , which block the stream and throw exceptions InterruptedException . This is how your run() should be (without this ):

      public void run() { try { Thread.sleep(10000); } catch (InterruptedException ie) { this.interrupt(); System.out.println(this.isInterrupted()); System.out.println("T1"); } } 
    • That is, the flag has changed but a clearance has occurred and the flag has been reset again? - Yurii Yusko
    • If there was no sleep() , we ourselves would check the status of the flag. And so, this happened in the sleep() method, which interrupted its "sleepy work", that is, it had already processed our request to "interrupt". He broke off, but how could he tell the outside world that he was interrupted? By throwing an exception. But he dropped the flag, because already processed it. Many people for some reason think that interrupt() throws InterruptedException and we can intercept it with catch() , but see for yourself the interrupt() method - do you see there throw new InterruptedException ? Not. The exception in this example is throwing the sleep() method - Oleksiy Morenets
    • one
      Where did Exception come from, I knew, but for the nuance of dropping the flag, MANY THANKS! - Yurii Yusko