ThreadDeath error - calling stop method on thread throws ThreadDeath error in java


Calling stop method on thread throws ThreadDeath error. It’s important to note ThreadDeath is error not exception
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ThreadDeathDemo {
   public static void main(String[] args) throws InterruptedException {
          Thread thread1 = new Thread("thread-1") {
                 public void run() {
                       try {
                              System.out.println(Thread.currentThread().getName()
                                            + " has started");
                              //calling stop method throws ThreadDeath error.
                              stop();

                       } catch (ThreadDeath e) {
                              System.out.println(Thread.currentThread().getName()
                                            + " has died");
                       }
                 }
          };
          thread1.start();
   }
}
/*OUTPUT
thread-1 has started
thread-1 has died
*/

Must know :
ThreadDeath is an error which application must not try to catch but it is normal condition.

No comments:

Post a Comment