Stopping of a thread

Source: Internet
Author: User
Tags terminates thread stop

Package fengke.thread;
/**
* Thread Stop
* @author Feng Ke
* Content: There are three ways to stop a thread:
* 1. Use the exit flag to cause the thread to exit normally, that is, the thread terminates when the Run method completes.
* 2. Use the Stop method to forcibly terminate a thread (this method is deprecated, because stop and suspend, resume, and so on, can also cause unpredictable results).
* 3. Use the interrupt method to break the thread.
*
*/

public class Threadstop extends thread{
public volatile Boolean exit=true;
@Override
public void Run () {
while (exit) {
System.out.println (GetName () + "thread is running ....");
}
System.out.println ("thread ends the first stage!!! ");
}

public static void Main (string[] args) {
Stopthread ();
System.out.println ("********stop () *********");
StopThread2 ();
System.out.println ("********interrupt () *********");
StopThread3 ();


}
Use Flag to exit
public static void Stopthread () {
/*
* 1. Use the exit flag to cause the thread to exit normally, that is, the thread terminates when the Run method completes.
* 2. Using the Join () method, the thread is preempted by the first execution,
* Can pass the SYSTEM.OUT.PRINTLN ("thread ends the second stage!!! ")
* SYSTEM.OUT.PRINTLN ("thread ends the first stage!!! ")
* The location to see the difference
* 3. It's best to end the process in this way
*/
Threadstop ts = new Threadstop ();
Ts.setname ("Mark");
Ts.start ();
try {
Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Ts.exit=false;
try {
Ts.join ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("thread ends the second stage!!! ");
System.out.println ("***********************");

}
Terminating with Stop ()
public static void StopThread2 () {

Threadstop ts = new Threadstop ();
Ts.setname ("Stop");
Ts.start ();
try {
Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Ts.stop ();
System.out.println ("***********************");

}

/*
* Using the Interrupt method
* Similar to Logo
* Note that in the terminating thread, it is best not to have sleep () and other methods, otherwise there will be a break failure
* Definition of interrupt:
*
* The isinterrupted () method simply checks the interrupt state of the current thread, but does not clear the state
* static method Interrupted () This method also detects the interrupt state of the current thread, but this method produces a side effect that clears the current thread's interrupt state.
*
* Thread.Interrupt () VS thread.stop () The biggest difference between the two methods is:
* Interrupt () method is to set the interrupt state of the thread, let the user choose the time and place to end the thread;
* The Stop () method throws a Threaddeath error directly at the run of the code, which is a java.lang.Error subclass.
* So direct use of the Stop () method is likely to cause an inconsistency of the object.
*
* Interrupt () does not interrupt a running thread. This approach is actually done by throwing an interrupt signal when the thread is blocked,
* This allows the thread to exit the blocked state. More specifically, if the thread is blocked by one of the three methods of Object.wait, Thread.Join and Thread.Sleep,
* Then, it will receive an interrupt exception (interruptedexception), thus terminating the blocked state early. If the thread is not blocked,
* Calling interrupt () will not work, otherwise the thread will get an exception (the thread must be prepared to handle the condition beforehand) and then flee the blocking state.
* Thread A when executing sleep,wait,join, thread B calls A's interrupt method, indeed this time a will have a interruptedexception exception thrown.
* But this is actually in sleep,wait,join these methods internally will constantly check the value of the interrupt state, while the interruptedexception itself throws.
* If thread A is performing some specified operations such as assignment, For,while,if, calling method, etc., it will not check the interrupt state, so thread A does not throw interruptedexception,
* and will always perform their own operations. When thread a finally executes to wait (), sleep (), join (), the interruptedexception is immediately thrown.
* If there is no call to sleep (), wait (), join () These methods, or no online thread themselves to check the interrupt state to throw interruptedexception,
* That interruptedexception is not to be thrown out.
*
*/
public static void StopThread3 () {
Threadstopinterrupt ts = new Threadstopinterrupt ();
Ts.setname ("interrupt");
Ts.start ();
try {
Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Ts.interrupt ();
System.out.println ("***********************");
try {
Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Interrupt method End");
}

}
Class Threadstopinterrupt extends thread{
public void Run () {
while (!this.interrupted ()) {
System.out.println (GetName () + "thread is running ....");
Some methods, such as using sleep () in a thread, can cause the interrupt to fail
try {
Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
//   }
}
System.out.println (GetName () + "thread end ....");
}
}

Stopping of a thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.