Three ways to stop a thread
1. Set flag bit (unable to handle the problem of thread blocking);
2. Call the stop method provided by the Thread class to forcibly close the Thread;
3. interrupt method provided by Thread class
Example: set flag bit
class InterruptThread extends Thread{ private boolean flag = true; public void setFlag(boolean flag) { this.flag = flag; } @Override public void run() { int i =0; while(flag){ i++; System.out.println(Thread.currentThread().getName()+",i= "+i); } } } public class TestInterrupt { public static void main(String[] args) throws InterruptedException { InterruptThread t1 = new InterruptThread(); t1.start(); //In order to see the effect, sleep for 1s. Thread.sleep(1000); t1.setFlag(false); } }
Example: using stop
class InterruptThread extends Thread{ private boolean flag = true; public void setFlag(boolean flag) { this.flag = flag; } @Override public void run() { int i =0; while(flag){ i++; System.out.println(Thread.currentThread().getName()+",i= "+i); } } } public class TestInterrupt { public static void main(String[] args) throws InterruptedException { InterruptThread t1 = new InterruptThread(); t1.start(); //In order to see the effect, sleep for 1s. Thread.sleep(1000); t1.stop(); } }
Using the stop method is not recommended: (1) the method has expired; (2) incomplete data will be generated.
Example: using interrupts (non blocking)
class InterruptThread extends Thread{ private boolean flag = true; public void setFlag(boolean flag) { this.flag = flag; } @Override public void run() { int i =0; while(flag){ //View current interrupt status boolean bool = Thread.currentThread().isInterrupted(); System.out.println(bool); i++; System.out.println(Thread.currentThread().getName()+",i= "+i); } } } public class TestInterrupt { public static void main(String[] args) throws InterruptedException { InterruptThread t1 = new InterruptThread(); t1.start(); //In order to see the effect, sleep for 1s. Thread.sleep(1000); t1.interrupt(); } }
The result shows that interrupt does not interrupt the thread, but changes the original interrupt flag from false to true. This is in the case of non blocking.
Example: use interrupt (in case of blocking)
class InterruptThread extends Thread { private boolean flag = true; public void setFlag(boolean flag) { this.flag = flag; } @Override public void run() { int i = 0; while (flag) { try { Thread.sleep(200); boolean bool = Thread.currentThread().isInterrupted(); System.out.println("The current thread status is:" + bool); System.out.println(Thread.currentThread().getName() + ",i = " + i++); } catch (InterruptedException e) { System.out.println("Throw interrupt exception"); boolean bool = Thread.currentThread().isInterrupted(); System.out.println(bool); //false return; } } System.out.println("Thread stop"); } } public class TestInterrupt { public static void main(String[] args) throws InterruptedException { InterruptThread t1 = new InterruptThread(); t1.start(); //In order to see the effect, sleep for 1s. Thread.sleep(1000); t1.interrupt(); } }
The result shows that in the case of blocking (sleep), an interrupt exception will be thrown directly, and the interrupt flag true will be changed back to false.
Summarize the third:
1) if the thread is non blocking (similar to sleep/wait/join is not used), calling the thread object's interrupt method will not interrupt the thread, but simply change the thread's state from false to true. We can further determine how to handle the thread according to this state.
2) Ruo Xiancheng called the method of blocking threads, such as sleep/wait/join, which throws InterruptedException when calling the interrupt method of the thread. At the same time, restore the thread state to false.