Interrupt may cause "thread interruption", but it cannot guarantee "thread interruption ".
The "interrupt thread" here means "Stop thread", but why not "Stop thread?
The thread has a clear Stop method, but it is opposed to the use, so please remember not to mention it later in Java
Stop the thread. Forget it!
Why not use "Stop thread.
[Stop a thread]
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. Here the "immediate" is too "immediate", just like
The toy child heard the adult say "go to bed" and put the toy in the ground to go to bed immediately. Such a child is not good.
Assume that a thread is executing:
Synchronized void {
X = 3;
Y = 4;
}
Because the methods are synchronous, X and Y are always assigned values when multiple threads access them.
When x = 3;, the STOP () method is called. Even in the synchronization block, it simply stops, resulting in incomplete
And the most basic condition in multi-threaded programming to ensure data integrity, so please forget the thread stop
In the future, we will never say "stop the thread" again.
How can we "end" a thread?
[Interrupt thread]
To end a thread, we need to analyze the running status of the thread, that is, what the thread is doing. If the child
If nothing is done, let him go to bed immediately. If the child is playing with his toys, let him play.
Clean up and go to bed.
Therefore, a thread should have three phases from running to actually ending:
1. Normal operation.
2. process the work before the end, that is, prepare to finish.
3. End and exit.
After an SQL logic in JDBC ends, you must disable connnection anyway.
That is done in the finally clause. Similarly, the job before the end of the thread should be done in finally to ensure that the job before the thread exits.
Make sure:
Try {<br/> logic <br/>} catch () {}< br/> finally {<br/> CLEANING <br/>}
So how can we end a thread? Since stop cannot be called, only the interrupt () method is available. But interrupt ()
The method only changes the running status of the thread. How can I exit the running process?
For general logic, as long as the thread status is interrupted, we can let it exit, so such a statement can ensure
The thread can stop running after being interrupted:
While (! Isinterrupted ()){
Normal Logic
}
In this way, if the thread is called the interrupt () method and isinterrupted () is true, the thread will exit the operation.
If the thread executes the wait, sleep, and join methods in "normal operation", you call the interrupt () method, and the logic is incomplete.
If:
Public void run () {<br/> try {<br/> while (! Isinterrupted () {<br/> normal operation <br/>}< br/> catch (exception e) {<br/> return; <br/>}< br/> finally {<br/> cleanup <br/>}</P> <p>}
We can see that if the thread execution logic is set to true after innterrupt is called
The finally cleanup process ends. Even if the thread is wait, sleep, or join, an exception is thrown and the Catch Block is jumped,
Return jumps out of the loop, and finally enters finally to execute the cleanup and exit.
Note that return should be used in the catch block to jump out of the loop. If nothing is done, it is like the following problematic code:
Public void run () {</P> <p> while (! Isinterrupted () {<br/> try {<br/> working properly <br/>} catch (exception E) {<br/> // nothing <br/>}< br/> finally {</P> <p >}< br/>}
In (1), we know that wait, sleep, and joinInterrupt ()In the catch where interruptedexception is thrown
There are two solutions: returning to normal or committing suicide.
1. [enable the thread to continue execution], that is, the logic after waking up in the catch statement, or the catch statement
Back to the normal logic. In short, it is from the wait, sleep, join pause State to live, back to normal.
2. [You can directly stop the thread running]. Of course, nothing is handled in catch, or return, then the process will be completed.
The mission of the current thread, which can immediately "stop" in the "paused" state above.
In the above Code, the wait method checks that isinterrupted () is true and throws an exception, but you have not processed it.And one throws
The status of the interruptedexception thread will be immediately set to non-interrupted,If the catch statement does not handle an exception
In the next loop, if isinterrupted () is false, the thread continues while execution. You may throw an exception n times and cannot stop the thread.
So how can we ensure that the thread is actually stopped?
During thread synchronization, we have a "double check", which can improve the efficiency and
Ensure that the thread is under real synchronization control.
So I call the correct thread exit method "Double secure exit", that is, it does not take isinterrupted () as the loop condition.
Use a tag as the cycle condition:
Override the interrupt method of Thread
Class mythread extend thread {<br/> private Boolean isinterrupted = false; // this sentence will be modified later <br/> Public void interrupt () {<br/> isinterrupted = true; <br/> super. interrupt (); <br/>}< br/> Public void run () {</P> <p> while (! Isinterrupted) {<br/> try {<br/> normal operation <br/>} catch (exception E) {<br/> // nothing <br/>}< br/> finally {</P> <p >}< br/ >}