java Multi Thread Stop Thread Method Example Code Detailed Explanation

  • 2021-07-26 07:34:38
  • OfStack

Three methods related to thread stopping


/*
 Interrupt the thread. If a thread is wait(),join(),sleep() And other methods block, call interrupt() The thread interrupt state is cleared and received InterruptedException Abnormal. In addition interrupt(); For isAlive() Return false The thread of does not work. 
*/
 public void interrupt();
 /*
  Static method, which judges the thread interrupt state and clears the thread interrupt state. So the method is called several times in a row, and the 2 You must return after the second time false . In addition, isAlive() Used to determine whether the thread is alive, if isAlive() Return false , interrupted() Also must return false . 
 */
 public static boolean interrupted();
/*
 Determines the thread interrupt state, but does not clear the thread interrupt state. In addition, isAlive() Used to determine whether the thread is alive, if isAlive() Return false , interrupted() Also must return false . 
*/

public boolean isInterrupted (); Several situations where threads stop:

1: With the exit flag, the run method is finished and the thread exits normally.

2: Use stop () method, which is outdated and not recommended.

3: Use the interrupt () method to interrupt the thread.

interrupt()

Calling this method alone does not interrupt the thread, but only marks an interrupt state. Or change the thread state to interrupt state.

You can interrupt threads in the following ways.

Throw an exception inside the thread. End the thread with return inside the thread.

Summarize

Above is this site to introduce you to java multi-threaded stop thread method example code details, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: