When a method is declared that might throw a interruptedexception exception, the method is described as a method that may take a little time, but can be canceled.
The representative methods of throwing interruptedexception are:
1. The wait method for the Java.lang.Object class
2. The sleep method of the Java.lang.Thread class
3. Join method for the Java.lang.Thread class
The representative methods of throwing interruptedexception are:
1. The wait method for the Java.lang.Object class
2. The sleep method of the Java.lang.Thread class
3. Join method for the Java.lang.Thread class
-A way to take some time
The thread that executes the wait method enters the waiting area waiting to be notify/notify all. During the wait, the thread will not be active.
The thread that executes the sleep method pauses the time set in the execution parameter.
The thread that executes the join method waits until the specified thread ends.
Therefore, the method above is a way to take some time.
--Methods that can be canceled
Because the action that takes time reduces the responsiveness of the program, it may cancel/abort the execution of this method.
This is mainly done by the interrupt method to cancel.
1. Sleep method and Interrupt method
The interrupt method is an instance method of the thread class, which does not need to acquire a lock on the thread instance, and any thread can invoke the interrupt method of the other thread at any moment through the thread instance.
When a thread in sleep is called the interrupt method, the paused state is discarded and the interruptedexception exception is thrown, so that the control of the thread is given to the catch block that catches the exception.
2. The wait method and the interrupt method
When a thread calls the wait method, it touches the lock when it enters the waiting area. When the interrupt method is called on a thread in wait, the lock is retrieved first, then the interruptedexception exception is thrown, and the interruptedexception exception cannot be thrown until the lock is acquired.
3. Join method and Interrupt method
When a thread waits for another thread to end with the Join method, it can be canceled by using the interrupt method. Because the join method does not need to acquire a lock, as with sleep, it jumps immediately to the catch block
--What did the interrupt method do?
The interrupt method actually changes the interrupt state only.
The inside of these methods, sleep, wait, and join, constantly checks the value of the interrupt state to throw interruptedexception.
So, if the thread does other processing, it calls its interrupt method, and the threads do not throw interruptedexception, only when the thread goes to sleep, wait, join these methods, Before the interruptedexception is thrown. If you do not call sleep, wait, join these methods, or do not have the online thread themselves check the interrupt state, throw interruptedexception, that interruptedexception will not be thrown out.
Isinterrupted method, which can be used to check the interrupt status
The Thread.interrupted method can be used to check and clear the interrupt state.
From: 4845757
Java Thread Interruptedexception exception