![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Runtime removeShutdownHook() Method
Description
The Java Runtime removeShutdownHook(Thread hook) method de-registers a previously-registered virtual-machine shutdown hook.
Declaration
Following is the declaration for java.lang.Runtime.removeShutdownHook() method
public boolean removeShutdownHook(Thread hook)
Parameters
hook − the hook to remove
Return Value
This method returns true if the specified hook had previously been registered and was successfully de-registered, false otherwise.
Exception
IllegalStateException − If the virtual machine is already in the process of shutting down
SecurityException − If a security manager is present and it denies RuntimePermission("shutdownHooks")
Example: Removing Shutdown Hook to a Thread Object
The following example shows the usage of Java Runtime addShutdownHook() method. In this program, we've created one static inner class Message which is extending Thread. In main method, we've registered a shutdown hook with a new Message object using addShutdownHook() method. Then we kept system to sleep for 3 seconds.
Now shutdown hook is removed using removeShutdownHook() method and then printed a closing message. As shutdown hook is registered with Message object, its run method should be called when program exits but as removeShutdownHook() removed the shutdown hook, bye message is not printed on console when program exits.
package com.tutorialspoint; public class RuntimeDemo { // a class that extends thread that is to be called when program is exiting static class Message extends Thread { public void run() { System.out.println("Bye."); } } public static void main(String[] args) { try { Message p = new Message(); // register Message as shutdown hook Runtime.getRuntime().addShutdownHook(p); // 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); // remove the hook Runtime.getRuntime().removeShutdownHook(p); // print that the program is closing System.out.println("Program is closing..."); } catch (Exception e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Program is starting... Waiting for 3 seconds... Program is closing...