ThreadDeath in Java Programming

ThreadDeath in Java Programming

Previous Home Next

 

ThreadDeath class extends Error . It is defined ito java.lang.ThreadDeath .There no usage of java.lang.ThreadDeath. The ThreadDeath instance is thrown when the stop method with zero arguments is called.

 
   

 In the following code We had created a thread  of R4R class and start this thread but in between execution a method which cause stop this thread by calling stop() methods. Then what we have to do basically? We need cleanup all required resources. So now we can catch it by using try-catch block. Here ThreadDeath class, which is subclass of Error, will catch it and into catch block we can write the cleanup code. After that we can re-throw the errors.

  public class  R4RThreadDeathExample  {

    public static void  main(String argv[]) {

        Thread  t = new Thread(new R4R());

        try {

            t.start();

            method(t);

        } catch (ThreadDeath  td) {

           
        // do some required cleanup

            throw td;     // re-throw the error

        }

    }

}
Previous Home Next