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.


//from w w w .  ja v  a  2  s.c om

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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable