The stop () method is not recommended.
The notorious stop () method of stopping a thread is no longer recommended. Why?
When the stop () method is called on a thread object, the thread running on this thread object will immediately stop and throw a special threaddeath () exception. The "immediate" here is too "immediate,
Assume that a thread is executing:
synchronized void {
x = 3;
y = 4;
}
Because the method is synchronous, X and Y are always assigned values when multiple threads access the data. If a thread is running to X = 3, stop () is called () method, even in the synchronization block, it simply stops, resulting in incomplete disability data. The most basic condition in multi-threaded programming must ensure data integrity. Therefore, please forget the stop method of the thread. In the future, we will never say "stop the thread" again.
How can I end a thread?
Interupt () interrupt thread
A thread has three phases from running to actually ending:
- Normal operation.
- Process the work before the end, that is, prepare to finish.
- End and exit.
So how can we end a thread? Since stop cannot be called, only the interrupt () method is available. However, the interrupt () method only changes the running state of the thread. How can I let it exit? For general logic, as long as the thread state has been interrupted, we can let it exit. Here we define a Thread class threada, so such a statement can ensure that the thread can stop running after being interrupted:
While (! Isinterrupted ()){
Normal Logic
}
, A test class, threaddemo
In this way, threaddemo calls the interrupt () method, and isinterrupted () is true, and the operation will exit. However, if the thread is executing the wait, sleep, and join methods and you call the interrupt () method, the logic is incomplete.
We can handle it like this:
Public void run (){
While (! Isinterrupted ()){
Try {
Normal Operation
} Catch (interruptedexception e ){
// Nothing
}
}
}
}
Think about what would happen to a sleep thread after calling interrupt? The wait method checks that isinterrupted () is true and throws an exception, but you have not processed it. The status of a thread that throws interruptedexception is immediately set to non-interrupted. If the catch statement does not handle any exception, isinterrupted () is false in the next loop, the thread continues to run. You may throw an exception n times and cannot stop the thread.
Instance code for this error
Threada
Public class threada extends thread ...{
Int COUNT = 0;
Public void run ()...{
System. Out. println (getname () + "going to run ...");
While (! This. isinterrupted ())...{
System. Out. println (getname () + "running" + Count ++ );
Try ...{
Thread. Sleep (400 );
} Catch (interruptedexception e )...{
System. Out. println (getname () + "exit from blocking ...");
System. Out. println ("This. isinterrupted () =" + this. isinterrupted ());
}
}
System. Out. println (getname () + "terminated! ");
}
}
Threaddemopublic class threaddemo ...{
Public static void main (string argv []) throws interruptedexception ...{
Threada TA = new threada ();
Ta. setname ("threada ");
Ta. Start ();
Thread. Sleep (2000 );
System. Out. println (TA. getname () + "being interrupted ...");
Ta. Interrupt ();
System. Out. println ("ta. isinterrupted () =" + Ta. isinterrupted ());
}
}
So how can we ensure that the thread is actually stopped? During thread synchronization, we have a "double check", which can ensure the real synchronization control of the thread on the basis of Improving the efficiency. So I call the correct exit method of the thread "Double secure exit", that is, it does not take isinterrupted () as the loop condition. A tag is used as the cyclic condition:
The correct threada code is:
Public class threada extends thread ...{
Private Boolean isinterrupted = false;
Int COUNT = 0;
Public void interrupt ()...{
Isinterrupted = true;
Super. Interrupt ();
}
Public void run ()...{
System. Out. println (getname () + "going to run ...");
While (! Isinterrupted )...{
System. Out. println (getname () + "running" + Count ++ );
Try ...{
Thread. Sleep (400 );
} Catch (interruptedexception e )...{
System. Out. println (getname () + "exit from blocking ...");
System. Out. println ("This. isinterrupted () =" + this. isinterrupted ());
}
}
System. Out. println (getname () + "terminated! ");
}
}