| The literal translation of Two-phasetermination is "two-phase termination". However, in this mode, the translation "two-phase termination" is more appropriate. Imagine that you have a thread running periodically, in the "operation phase", you sent a request to stop the execution thread. At this time, the execution thread should not terminate the current job in a panic manner, but complete the work of this cycle first, then, go to the "aftercare" stage to complete some aftercare work, such as closing archives or network streaming. The so-called two-phase termination means suspending the "operation phase" and completing the "aftercare phase ", complete execution tasks. To terminate a thread in Java, we do not recommend that you use the stop () method directly to terminate the execution thread. The stop () method will throw the threaddeath exception and force the execution thread to terminate, even if the execution thread is in operation or is executed in the synchronized zone, you are advised to terminate the execution thread on your own, for example: Public class somethread extends thread { Private Boolean isterminated = false; Public void terminate () { isterminated = true; } Public void run () { while (! Isterminated) { //... some statements } } } Considering that sometimes the execution thread may run to sleep () or wait () and enter the notrunnable State, using the above method may delay the request to terminate, therefore, you can call the interrupt () method at the request of termination. This will throw interruptedexception and cause the execution thread to exit from the not runnable State. Therefore, you can change the program: Public class somethread extends thread { Private Boolean isterminated = false; Public void terminate (){ Isterminated = true; Interrupt (); } Public void run (){ Try { While (! Isterminated ){ //... Some statements } } Catch (interruptedexception e ){ } } } After a stop request is sent, if the execution thread is in the notrunnable state, interruptedexception will be thrown. If this exception is not caught first, it will be captured by the catchinterruptedexception in run, that is to say, it will leave the while loop directly. Therefore, if you want to finish the work in this cycle after sending a termination request, you must first capture this exception, if you do not need to complete this cycle of work, you do not need to capture this exception. How to do it depends on your program. If the execution thread is to complete the work of this cycle, check the flag before the next cycle starts. The result is false, so you can leave the while loop and perform some follow-up work at this time, this can be written in the Finally block, for example: Public class somethread extends thread { Private Boolean iscontinue = false; Public void terminate (){ Isterminated = true; Interrupt (); } Private void doworkbeforeshudown (){ //... Do some work before shutdown } Public void run (){ Try { While (! _ Isterminated ){ //... Some statements } } Catch (interruptedexception e ){ } Finally { Doworkbeforeshudown (); } } } The above program is basically a two-phasetermination architecture. In addition, if your thread is still serving other objects, before sending a termination request to completely terminate, the service should be stopped for other objects. You can query whether the execution thread has been terminated before other objects require the service. This can be achieved by providing a method: Public class somethread extends thread { Private Boolean isterminated = false; Public void terminate (){ Isterminated = true; Interrupt (); } Public Boolean isterminated (){ Return _ isterminated; } Private void doworkbeforeshudown (){ //... Do some work before shutdown } Public void run (){ Try { While (! _ Isterminated ){ //... Some statements } } Catch (interruptedexception e ){ } Finally { Doworkbeforeshudown (); } } } |