GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - JVM Shutdown Hook
Selected Reading
Java - JVM Shutdown HookJVM ShutdownThe following are the two different ways for JVM shutdown
JVM Shutdown HookA shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method. JVM Shutdown Hook: The addShutdownHook(Thread hook) MethodThe Runtime addShutdownHook(Thread hook) method registers a new virtual-machine shutdown hook. DeclarationFollowing is the declaration for java.lang.Runtime.addShutdownHook() method public void addShutdownHook(Thread hook) Parametershook − An initialized but unstarted Thread object. Return ValueThis method does not return a value. Exception
Example of JVM Shutdown HookIn this example, we're creating a class CustomThread by extending Thread class. This thread object will be used as JVM Shutdown hook. CustomThread class has run() method implementation. In main class TestThread, we've added a shutdown hook using Runtime.getRuntime().addShutdownHook() method, by passing it a thread object. In output you can verify that CustomThread run() method is called when program is about to exit. package com.tutorialspoint;
class CustomThread extends Thread {
public void run() {
System.out.println("JVM is shutting down.");
}
}
public class TestThread {
public static void main(String args[]) throws InterruptedException {
try {
// register CustomThread as shutdown hook
Runtime.getRuntime().addShutdownHook(new CustomThread());
// print the state of the program
System.out.println("Program is starting...");
// cause thread to sleep for 3 seconds
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
// print that the program is closing
System.out.println("Program is closing...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
OutputProgram is starting... Waiting for 3 seconds... Program is closing... JVM is shutting down. More Example of JVM Shutdown HookExample 1In this example, we're creating a class CustomThread by implementing Runnable interface. This thread object will be used as JVM Shutdown hook. CustomThread class has run() method implementation. In main class TestThread, we've added a shutdown hook using Runtime.getRuntime().addShutdownHook() method, by passing it a thread object. In output you can verify that CustomThread run() method is called when program is about to exit. package com.tutorialspoint;
class CustomThread implements Runnable {
public void run() {
System.out.println("JVM is shutting down.");
}
}
public class TestThread {
public static void main(String args[]) throws InterruptedException {
try {
// register CustomThread as shutdown hook
Runtime.getRuntime().addShutdownHook(new Thread(new CustomThread()));
// print the state of the program
System.out.println("Program is starting...");
// cause thread to sleep for 3 seconds
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
// print that the program is closing
System.out.println("Program is closing...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
OutputProgram is starting... Waiting for 3 seconds... Program is closing... JVM is shutting down. Example 2We can remove the shutdown hook as well using removeShutdownHook(). In this example, we're creating a class CustomThread by implementing Runnable interface. This thread object will be used as JVM Shutdown hook. CustomThread class has run() method implementation. In main class TestThread, we've added a shutdown hook using Runtime.getRuntime().addShutdownHook() method, by passing it a thread object. As last statement, we're removing the hook using Runtime.getRuntime().removeShutdownHook() method package com.tutorialspoint;
class CustomThread implements Runnable {
public void run() {
System.out.println("JVM is shutting down.");
}
}
public class TestThread {
public static void main(String args[]) throws InterruptedException {
try {
Thread hook = new Thread(new CustomThread());
// register Message as shutdown hook
Runtime.getRuntime().addShutdownHook(hook);
// print the state of the program
System.out.println("Program is starting...");
// cause thread to sleep for 3 seconds
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
// print that the program is closing
System.out.println("Program is closing...");
Runtime.getRuntime().removeShutdownHook(hook);
} catch (Exception e) {
e.printStackTrace();
}
}
}
OutputProgram is starting... Waiting for 3 seconds... Program is closing... Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |