Java multi-thread design mode (4)

Source: Internet
Author: User

Java multi-thread design model Reading Notes 4 -- two-phase termination

In the two-phase termination mode, the thread ends normally, that is, some aftercare is performed before the end, and the released resources are released to complete the current task. In Java, there is a method to stop. This method will end the current thread, but this method should not be used because it will cause disastrous consequences. Use the flag setting method instead of the Stop method.

Turn to a blog http://www.blogjava.net/jesson2005/articles/111202.html about this pattern

Two-phase termination is called "Two-Phase termination". However, in this mode, the "two-phase termination" parameter is equivalent, imagine that you have an ongoing operation. In the "operation segment", you have sent a request to stop the operation, at this time, the Bank should not panic about the current work, but finish the work in this phase first, then enter the "post-Good" section to complete some post-Good work, such as the Case Concerning Website or online streaming, that is, stop the operation segment and complete the operation.

In terms of Java thread termination, it is not recommended that you use the stop () method directly to stop the pause row commit, stop () the method causes the threaddeath exception to force the primary row to stop, even if the primary row is running as a segment or the primary row is in the synchronized area, if you want to stop the primary row, the suggestions are implemented on their own, for example:

Public class somethread extends thread {
Private Boolean isterminated = false;

Public void terminate (){
Isterminated = true;
}

Public void run (){
While (! Isterminated ){
//... Some statements
}
}
}

When the test is completed, the dynamic route entry may be written to sleep () or wait () and not runnable Dynamic Route entry, using the above method may extend the period-and-period request. Therefore, you can call the interrupt () method when the period is required. This produces interruptedexception, so that the primary row crashes from the not runnable primary keys, 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 sending the abort request, if the failed row is not runnable failed, interruptedexception will be generated. If this exception is not captured first, it will be run () in the catch interruptedexception capture, that is, it will directly open the while loop. Therefore, if you are requesting to finish the work in this phase after sending a response, you need to capture this exception first. If you do not need to finish the work in this phase, you do not need to capture this exception. How can you decide your program.

If you want to complete the job in this scheduling period, check the flag before the start of the next scheduling period. At this time, the result is false, so when you open the while loop, you can do some good work in this time. This can be done in the finally region, 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 ();
}
}
}

In general, the above program is the architecture of the two-phase termination mode. In addition, if your zookeeper still needs other objects, before sending an end-to-end request to the end of the request, you should stop the service for other objects. You can make other objects require the service, first, check whether the specified row has been requested to be stopped. 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
}< BR >}< br> catch (interruptedexception e) {
}< br> finally {
doworkbeforeshudown ();
}< BR >}

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.