In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to stop a thread task that is running in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Basic knowledge of thread stop
Interrupted (): tests whether the current thread has been interrupted. This method is static and returns a Boolean value when called. However, the state of the thread will be changed after the call, and if it is called in the interrupted state, the interrupted state of the thread will be cleared after the call.
IsInterrupted (): test whether the thread has been interrupted. The method is called by the object
Interrupt (): marks the thread as interrupted, but does not interrupt the running thread.
Stop (): violently stops the thread. Has been deprecated.
Method 1: stop the thread by exception method
After the thread calls the interrupt () method, it judges the interrupted () state of the current object in the thread's run method, and throws an exception if it is an interrupt state, thus achieving the effect of interrupting the thread.
The following is an example:
MyThread.java
Public class MyThread extends Thread {@ Override public void run () {try {for (int I = 0; I < 500000; iTunes +) {if (MyThread.interrupted ()) {System.out.println ("it's stopped, I'm quitting!") ; throw new InterruptedException ();} System.out.println ("I =" + (item1));} System.out.println ("if I am exported, it means the thread did not stop");} catch (InterruptedException e) {System.out.println ("captured in the run method in the MyThread class") E.printStackTrace ();}
Main.java
/ * * exit the for loop according to the interrupt status * @ Author: xjf * @ Date: 13:27 * / public class Main {public static void main (String [] args) {try {MyThread myThread = new MyThread (); myThread.start (); Thread.sleep (100); myThread.interrupt () } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("end!");}}
The results are as follows:
I = 19115
I = 19116
I = 19117
I = 19118
I = 19119
End!
It's stopped. I'm quitting!
Captured in the run method in the MyThread class
Java.lang.InterruptedException
At com.book.interrupt_exit.MyThread.run (MyThread.java:15)
Process finished with exit code 0
Method 2: stop the thread in a deep sleep
Sleep the thread first, then call interrupt to mark the interrupt state, and interrupt interrupts the thread in the blocking state. An interrupt exception is thrown to achieve the effect of stopping the thread. The following is an example:
MyThread.java
Public class MyThread extends Thread {@ Override public void run () {try {System.out.println ("run-start"); Thread.sleep (5000); System.out.println ("run-end");} catch (InterruptedException e) {System.out.println ("stopped in a deep sleep! When entering catch, whether the thread is stopped: "+ this.isInterrupted (); e.printStackTrace ();}"
Main.java
Public class Main {public static void main (String [] args) {try {MyThread myThread = new MyThread (); myThread.start (); Thread.sleep (2000); System.out.println ("status:" + MyThread.interrupted ()); myThread.interrupt ();} catch (InterruptedException e) {e.printStackTrace ();}
Result
Run-start
Status: false
Java.lang.InterruptedException: sleep interrupted
Stopped in a deep sleep! When entering catch, whether the thread is stopped: false
At java.lang.Thread.sleep (Native Method)
At com.book.sleep_interrupt.MyThread.run (MyThread.java:13)
The thread first calls interrupt to mark the interrupted state, and then the thread sleeps. An interrupt exception is thrown to achieve the effect of stopping the thread. As follows:
MyThread1.java
Public class MyThread1 extends Thread {@ Override public void run () {try {for (int I = 0; I < 1000000; iTunes +) {System.out.println ("I =" + (iTun1));} System.out.println ("run begin") / / interrupt makes an interrupt flag, which does not interrupt the running thread at that time. When the thread is in a blocking state, it will interrupt / / so it will interrupt Thread.sleep (200000) after interrupt and then when it encounters sleep blocking; System.out.println ("run end") } catch (InterruptedException e) {System.out.println ("stop first, then encounter sleep! Enter catch! ") ; e.printStackTrace ();}
Main1.java
Public class Main1 {public static void main (String [] args) {MyThread1 myThread1 = new MyThread1 (); myThread1.start (); myThread1.interrupt (); System.out.println ("end!");}}
Results:
I = 99993
I = 99994
I = 99995
I = 99996
I = 99997
I = 99998
I = 99999
I = 100000
Run begin
Stop first, and then encounter sleep! Enter catch!
Java.lang.InterruptedException: sleep interrupted
At java.lang.Thread.sleep (Native Method)
At com.book.sleep_interrupt.MyThread1.run (MyThread1.java:19)
Fourth, stop thread method 3:stop () violence stop
The thread's call to the stop () method is violently stopped and the method is deprecated. This method will have negative consequences:
Forcing the thread to stop may prevent some rational work from being done.
The locked object is "unlocked", resulting in non-synchronous data processing and data inconsistency (for example, a method adds synchronized and has been processed for a long time, but before the end of processing, the thread stop (), the unfinished data will not be synchronized)
Method 4: stop the thread by using return
After the call to interrupt is marked as interrupt state, the current thread state is judged in the run method. If it is interrupted, the thread is return, which has the effect of stopping the thread.
Note: it is recommended to use the method of "throwing exception" to stop the thread, because the exception can also be thrown up in the catch block, so that the event of thread stop can be propagated.
Thank you for reading! This is the end of this article on "how to stop the thread task that Java is running". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 263
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.