1. Conclusion first

Interrupt () : Marks the thread represented by the object calling this method with a stop mark, not actually stopping the thread.

Interrupted () : Gets the interrupted status of the current thread and clears the status flag of the thread. Yes is a static method.

IsInterrupted () : Gets the thread represented by the object calling this method, without clearing the status flag of the thread. Is an instance method.

Now each method is introduced in detail one by one:

2. interrupt()

Let’s start with the interrupt() method and see what happens:

public class MainTest {
   @Test
   public void test() {
       try {
           MyThread01 myThread = new MyThread01();
           myThread.start();
           myThread.sleep(2000);
           myThread.interrupt();
       } catch (Exception e) {
           System.out.println("main catch");
           e.printStackTrace();
       }
   }
}

public class MyThread01 extends Thread {
   @Override
   public void run() {
       super.run();
       for (int i = 0; i < 500; i++) {
           System.out.println("i= "+ i); }}}Copy the code

Output result:

As you can see, the child thread has finished executing. Note that the interrupt() method does not stop a thread, as we said in the beginning; it simply notes a stop mark in the current thread.

So how do we know this stop sign? Interrupted() and isInterrupted()

3. Interrupted () and the isInterrupted ()

  • Public static Boolean interrupted()

  • Public Boolean isInterrupted()

These two methods are very similar, let’s use the program to see the difference in the use effect

Let’s look at the procedure for using interrupted().

@Test
public void test() { try { MyThread01 myThread = new MyThread01(); myThread.start(); myThread.sleep(1000); // line 7: thread.currentThread ().interrupt(); Mythread.interrupt (); // Thread.currentThread() // myThread.interrupted() calls currentThread().isinterrupted (true); System.out.println(());"Interrupt 1" + myThread.interrupted());
          System.out.println("Interrupt 2" + myThread.interrupted());
      } catch (InterruptedException e) {
          System.out.println("main catch");
      }
  System.out.println("main end");
}
Copy the code

Output result:

This shows that the thread has not stopped, and proves the meaning of the interrupted() method: Tests whether the current thread has interrupted. This thread is the main thread, which has never interrupted, so it prints false.

So how do you make the main thread interrupt? Comment out line 8 above and uncomment line 7 and run again. We get the following output:

As a result, the interrupted() method does determine whether the current thread (in this case the main thread) is stopped, but why is the second Boolean false? After interrupted() tests whether the current thread is interrupted and clears the status flag.

Because this clears the status flag after interrupted(), isInterrupted(true) is called, where the argument is true. So interrupted() clears the status flag.

Thread.currentthread ().interrupt(); thread.currentThread (); This causes the current thread to be marked with an interrupt flag, so the first call to interrupted() returns true. Because interrupted() clears the status flag, false is returned the second time the method is called.

That’s interrupted(), and finally let’s look at isInterrupted().

The difference between isInterrupted() and interrupted() is that isInterrupted() does not clear the status flag because the underlying isInterrupted() method takes false. The other is the thread it determines is represented by the object calling the method, in this case MyThread01.

Let’s modify the above code to see how it works:

@Test
public void test() { try { MyThread01 myThread = new MyThread01(); myThread.start(); myThread.sleep(1000); myThread.interrupt(); // Modify the following two lines. // The above code is mythread.interrupted (); System.out.println("Interrupt 1" + myThread.isInterrupted());
       System.out.println("Interrupt 2" + myThread.isInterrupted());
   } catch (InterruptedException e) {
       System.out.println("main catch");
       e.printStackTrace();
   }
   System.out.println("main end");
}
Copy the code

Output result:

The result is clear because isInterrupted() does not clear the status flag, so it prints true both times.

Refer to the article: www.cnblogs.com/hapjin/p/54…

PS: This article was originally published on the wechat official account “More than Java”.