The use of Interrupt threads of Java multithreading

  • 2020-04-01 01:46:15
  • OfStack

Interrupt method

Interrupt literally means to interrupt, but in Java the method thread.interrupt () actually notifies a Thread in a way that does not abort the Thread directly. It's up to the person writing the code to decide exactly what to do, and usually we abort the thread.

      If a thread gets stuck calling the wait(), wait(long), or wait(long, int) method of the Object class, or the join(), join(long), join(long, int), sleep(long), or sleep(long, int) method of the class, the broken state is cleared and it receives an InterruptedException.

      If the thread in interruptible channel (Java. Nio. Channels. InterruptibleChannel) on the I/O operation, then the channel will be shut down, the thread of the interrupted status will be set and the thread will receive a ClosedByInterruptException.

      If the thread in a Selector (Java. Nio. Channels. The Selector), in the threads of the interrupted status will be set, it will immediately return from select operations, and may have a non-zero value, like call the Selector wakeup method.

      If none of the previous conditions are saved, the thread's interrupt state is set.

      There is no need to interrupt a thread that is not active.

Detect broken

How interrupts are detected depends on what the thread is doing.

      If the thread calls a method that can throw InterruptException, it catches InterruptException and handles it in a catch block (usually by exiting the run method to interrupt the thread)

      If another method is called, thread.interrupted can be checked at idle to determine whether or not an interrupt signal has been received, and the interrupt signal is confirmed for processing. You can throw an InterruptException to stay with the previous method

The interrupted status

The thread interrupt mechanism is implemented using the interrupt state, an internal flag. The interrupt state is set when the thread's interrupt() method is called (see the interrupt instructions above).

 

There are two ways to get the interrupt status of a thread:

      Calls the static method thread.interrupted (), which clears the interrupted state of the current Thread in addition to returning the interrupted state of the current Thread. In other words, if the method is called twice in a row, the second call returns false (except when the current thread breaks again, after the first call has cleared the broken state, and before the second call checks the interrupted state).

      Calls the specified thread's isInterrupted() method, which only returns the interrupted state of the specified thread without affecting the thread's interrupted state.

There are two ways to clear the interrupted state of a thread:

      As described above, calling thread.interrupted ()

      When the thread's interrupt() method is called to throw InterruptedException, the thread's interrupted state is cleared, including the wait(), wait(long), or wait(long, int) methods of the Object class, or the thread's join(), join(long), join(long, int), sleep(long), or sleep(long, int) methods


Related articles: