Java Data Type Tutorial - Java Runtime .addShutdownHook ( Thread hook)








Syntax

Runtime.addShutdownHook(Thread hook) has the following syntax.

public void addShutdownHook(Thread hook)

Example

In the following code shows how to use Runtime.addShutdownHook(Thread hook) method.

/*w w  w.  j  av a  2  s  .  com*/

class Message extends Thread {

  public void run() {
    System.out.println("Bye.");
  }
}

public class Main {

  public static void main(String[] args) {
    try {
      // register Message as shutdown hook
      Runtime.getRuntime().addShutdownHook(new Message());


    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

The code above generates the following result.