I. API
public void interrupt()
public static boolean interrupted()
public boolean isInterrupted()
II. Description of interrupt
1. Introduction
The call of the following methods will make the current thread enter the blocking state, and the blocking can be interrupted by calling the interrupt method of the current thread
Object # wait # method
Thread # sleep # method
Thread # join # method
io# operation of interruptible channel #
The # wakeup # method of Selector #
Several other methods
All of the above methods will make the current thread into blocking, so this method is sometimes called interruptible method. Interrupting a thread does not mean the end of the thread's life cycle, but only the blocking state of the current thread.
Once the thread is interrupted in the case of blocking, an exception called InterruptedException will be thrown. This exception is like a signal to inform the current thread that it has been interrupted.
2. Actual combat
package concurrent; import java.util.concurrent.TimeUnit; public class ThreadInterrptu { public static void main(String[] args) { Thread thread = new Thread(() -> { try { TimeUnit.MINUTES.sleep(1); } catch (InterruptedException e) { System.out.println("I am be interruputed"); e.printStackTrace(); } }); thread.start(); try { TimeUnit.MILLISECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } thread.interrupt(); } }
3. Test
I am be interruputed
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at concurrent.ThreadInterrptu.lambda$main$0(ThreadInterrptu.java:9)
at java.lang.Thread.run(Thread.java:748)
4. Description
The program creates a thread and attempts to sleep for 1 minute, but it is interrupted by the main thread calling the # interrupt # method after about 2 milliseconds. The execution result of the program is # I am be interrupted.
If a thread is already in a dead state, an attempt to interrupt it will be ignored.
III. isInterrupted description
Interrupted , is a member method of , Thread , which mainly judges whether the current Thread is interrupted. This method is only a judgment on the interrupt identification of the current Thread and will not affect any change in the identification.
1. Code
package concurrent; import java.util.concurrent.TimeUnit; public class ThreadisInterrupted { public static void main(String[] args) { Thread thread = new Thread(() -> { while (true) { } }); thread.start(); try { TimeUnit.MILLISECONDS.sleep(2); System.out.println("Thread is interrupted?" + thread.isInterrupted()); } catch (InterruptedException e) { e.printStackTrace(); } thread.interrupt(); System.out.println("Thread is interrupted?" + thread.isInterrupted()); } }
2. Test
Thread is interrupted?false
Thread is interrupted?true
Make a slight change to the above code and change the loop execution to the interruptible method of "sleep", which will capture the interrupt signal and erase the "interrupt" flag.
package concurrent; import java.util.concurrent.TimeUnit; public class ThreadisInterrupted2 { public static void main(String[] args) { Thread thread = new Thread() { public void run() { while (true) { try { TimeUnit.MINUTES.sleep(1); } catch (InterruptedException e) { System.out.println("Thread is interrupted?" + isInterrupted()); } } } }; thread.setDaemon(true); thread.start(); try { TimeUnit.MILLISECONDS.sleep(2); System.out.println("Thread is interrupted?" + thread.isInterrupted()); } catch ( InterruptedException e) { e.printStackTrace(); } thread.interrupt(); try { TimeUnit.MILLISECONDS.sleep(2); System.out.println("Thread is interrupted?" + thread.isInterrupted()); } catch ( InterruptedException e) { e.printStackTrace(); } } }
The test results are as follows:
Thread is interrupted?false
Thread is interrupted?false
Thread is interrupted?false
Because the interruptible method "sleep" is used in the "run" method, it will capture the interrupt signal and erase the "interrupt" identification, so the program execution result is "false"
IV. description of interrupted
1. Description
Interrupted is a static method. Although it is also used to judge whether the current thread is interrupted, it is very different from the member method , isInterrupted , method. Calling this method will directly erase the interrupt ID of the thread. If the current thread is interrupted, the first call of , interrupted , method will return , true, And erase the interrupt identifier immediately, and the subsequent invocation will always return to false unless it is interrupted again.
2. Code
package concurrent; import java.util.concurrent.TimeUnit; public class ThreadisInterrupted3 { public static void main(String[] args) { Thread thread = new Thread() { public void run() { while (true) { System.out.println(Thread.interrupted()); } } }; thread.setDaemon(true); thread.start(); try { TimeUnit.MILLISECONDS.sleep(2); } catch ( InterruptedException e) { e.printStackTrace(); } thread.interrupt(); } }
3. Test
false
false
false
false
false
false
true
false
false
false
false
false
false
Appears only once