SoFunction
Updated on 2025-04-07

Easy way to interrupt and restart a Thread thread

Here is a brief summary (the general idea, if there is no debugging, there may be mistakes!):

pulbic class MyThread implemets Thread{

 @overide
 public void run{
 while(!()){
 }
 }
}



Thread mThread = new MyThread();


public static void startThread(){
 mThread = new MyThread(); 
  ();
}

public void stopThread(){
 if(mThread!=null){
 ();
 mThread = null;
 }
}

There must be a while loop in the run function to determine the interrupt state. When interrupting Thread, interrupt() must be called outside. Note: The interrupt() function only sets the interrupt flag, not a forced interrupt, so the interrupt flag needs to be detected continuously in run (())

In Java, after interrupting Thread, you cannot start directly. You must new a new instance, otherwise an error will be reported.

The simple way to interrupt Android and restart a Thread thread in the above article is all the content I share with you. I hope you can give you a reference and I hope you can support me more.