Handling of non-controllable exceptions in thread groups

Source: Internet
Author: User

Java Example:

package concurrency;import java.util.random;public class main6 {     public static void main (String[] args)  {         mythreadgroup threadgroup = new mythreadgroup ("MyThreadGroup");         task1 task = new task1 ();         for (int i = 0; i < 2;i++) {             thread t = new thread (ThreadGroup,task);             t.start ();         }    }}class mythreadgroup extends threadgroup{     /* must declare a constructor with arguments, because Threadgroup does not have a default constructor with no arguments */    public  Mythreadgroup (String name)  { &Nbsp;      super (name);     }    /* Overriding the Uncaughtexception method */     @Override     public void  Uncaughtexception (thread t, throwable e)  {         System.out.printf ("the thread %s has thrown an exception\n", T.getid ());         e.printstacktrace (System.out);         system.out.printf ("terminating the rest of the threads\n");         interrupt ();     }}class task1 implements  runnable{    /* in this method, we are going to trigger the aritmethicexception anomaly in order to achieve this goal,       * we divide 1000 by a random number, and when the random number generator generates 0, the exception is thrown */     @Override     public  void run () &NBSP;{&NBSP;&NBSp;      int result;        random  random = new random (Thread.CurrentThread (). GetId ());         while (true) {            result =  1000/((int) (random.nextdouble () *1000));             system.out.printf ("%s : %d\n",  thread.currentthread (). GetId (), result);             if (Thread.CurrentThread (). isInterrupted ()) {                 system.out.printf ("% D : interrupted\n ",  thread.currentthread (). GetId ());                 return;            &nbsP;}         }    }}

When a thread throws a non-catching exception, the JVM will look for 3 possible processors for this exception. First, look for the non-catching exception handler for the thread that throws the exception, and if the processor does not exist, the JVM continues to look for the non-catching exception handler for the thread group that this thread is in, which is what this section learns, and if it does not exist, the JVM will look for the default non-catching exception handler. If none of these processors exist, the JVM prints the exception information from the stack to the console and exits the program.

Handling of non-controllable exceptions in thread groups

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.