What happens when the main thread crashes while other threads continue to run (2)

Source: Internet
Author: User
Tags exit in thread stop
What happens when the main thread crashes and other threads continue to run? (2) -- Linux general technology-Linux programming and kernel information. Read the following for details. An auxiliary means to diagnose this crash is to capture the dependent threads that are notified of exceptions thrown by various threads before exiting. This is exactly what I did in Listing 2.

List 2. Example of notifying the client thread of an error
Import java. util. Vector;

Public class Server2 extends Thread {
Client2 client;
Int counter;

Public Server2 (Client2 _ client ){
This. client = _ client;
This. counter = 0;
}

Public void run (){
Try {
While (counter <10 ){
This. client. queue. addElement (new Integer (counter ));
Counter ++;
}
Throw new RuntimeException ("counter> = 10 ");
}
Catch (Exception e ){
This. client. interruptFlag = true;
Throw new RuntimeException (e. toString ());
}
}

Public static void main (String [] args ){
Client2 c = new Client2 ();
Server2 s = new Server2 (c );
C. start ();
S. start ();
}
}

Class Client2 extends Thread {
Vector queue;
Boolean interruptFlag;

Public Client2 (){
This. queue = new Vector ();
This. interruptFlag = false;
}

Public void run (){
While (! InterruptFlag ){
If (! (Queue. size () = 0 )){
ProcessNextElement ();
}
}

// Processes whatever elements remain on the queue before exiting.
While (! (Queue. size () = 0 )){
ProcessNextElement ();
}
System. out. flush ();
}

Private void processNextElement (){
Object next = queue. elementAt (0 );
Queue. removeElementAt (0 );
System. out. println (next );
}
}



Other options for handling thrown exceptions may be to call System. exit. This option makes sense when the main thread of the program crashes and other threads do not manage any critical resources. However, in other cases, this may be dangerous. For example, consider an example where one of the other threads is managing an open file. If this is the actual situation, only exiting the program will cause resource leakage.

Even in the preceding simple example, calling System. exit in the server thread causes the client to exit without processing any remaining elements in its queue.

As a matter of fact, Sun does not recommend the thread stop method. The Force stop of a thread will cause the resource to be in an inconsistent state, so the stop method destroys the language security model.

For more information about Sun, see references.

Summary
Here is a summary of this week's incorrect model:

Mode: single thread


Symptom: A locked multi-threaded program that has or does not have to print stack traces to standard errors.


Cause: Multiple program threads are waiting for input from a thread, and the thread exits after an uncaptured exception is thrown.


Treatment and prevention measures: place the exception handling code in the main thread to notify the dependent thread when the crash occurs.
References

Participate in this forum.


Read the explanation about why Thread. stop is not recommended.


Neel V. Kumar provides a way to debug multi-threaded Java in his article "multithreading in Java programs" (developerWorks, March 2000.


Peter Haggar's IBM PartnerWorld for Developers, "Optimizing concurrency in Java programming", is an excellent White Paper that discusses common problems caused by concurrent data access through multithreading.


For more information about writing multi-threaded Java programs, see Alex Roetter's article "Writing multi-threaded Java applications" (developerWorks, November February 2001 ).


For technical help on multithreading in your Java application, visit the multi-threaded Java programming forum.


Brian Goetz easily handles difficult thread problems in a three-part series.


The JUnit homepage provides links to many interesting articles about program testing methods and the latest JUnit version.


Use the Java debugging tutorial (developerWorks, February 2001) to get help with General debugging technologies.


Read all Eric's articles on diagnosing Java code, most of which focus on error patterns.


Find more Java references in the developerWorks Java technology area.



About the author
Eric Allen earned a bachelor's degree in computer science and mathematics from Cornell University and is a PhD candidate from the Java programming language team at Rice University. Eric was the Java developer leader for Cycorp, Inc. before returning to Rice to concentrate on his degree. He also hosted a forum for Java beginners on JavaWorld. His research includes the development of Semantic Models and static analysis tools for Java language at the source and bytecode levels. Eric also helped develop Rice's NextGen programming language compiler. NextGen is a Java extension that supports the extensive runtime type. You can contact Eric via eallen@cs.rice.edu.

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.