addShutdownHook(Thread): This method is available in the java.lang.Runtime class of Java.
Syntax:
void java.lang.Runtime.addShutdownHook(Thread hook)
This method takes one argument. This method registers a new virtual machine shutdown hook.
The Java virtual machine shuts down in response to two kinds of events:
1. The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked.
2. The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user log off or system shutdown.
Parameters: One parameter is required for this method.
hook: An initialized but unstarted Thread object.
Throws:
1. IllegalArgumentException - If the specified hook has already been registered, or if it can be determined that the hook is already running or has already been run.
2. IllegalStateException - If the virtual machine is already in the process of shutting down.
3. SecurityException - If a security manager is present and it denies RuntimePermission("shutdownHooks").
Approach
Java
package com.Runtime;public class RuntimeaddShutdownHook {public static void main(String[] args) {Runtime runtime = Runtime.getRuntime();Thread hook = new Thread();runtime.addShutdownHook(hook);System.out.println("Successfully adds shutdown hook");}}
Output:
Successfully adds shutdown hook
Some other methods of Runtime
addShutdownHook(Thread): This method registers a new virtual machine shutdown hook.
availableProcessors(): This method returns the number of processors available to the Java virtual machine.
exec(String): This method executes the specified string command in a separate process.
exec(String[]): This method executes the specified command and arguments in a separate process.
exec(String, String[]): This method executes the specified string command in a separate process with the specified environment.
exec(String[], String[]): This method executes the specified command and arguments in a separate process with the specified environment.
exec(String, String[], File): This method executes the specified string command in a separate process with the specified environment and working directory.
exec(String[], String[], File): This method executes the specified command and arguments in a separate process with the specified environment and working directory.
gc(): This method runs the garbage collector in the Java Virtual Machine.
Runtime.getRuntime(): This method returns the runtime object associated with the current Java application.
halt(int): This method forcibly terminates the currently running Java virtual machine.
load(String): This method loads the native library specified by the filename argument.
loadLibrary(String): This method loads the native library specified by the libname argument.
maxMemory(): This method returns the maximum amount of memory that the Java virtual machine will attempt to use.
removeShutdownHook(Thread): This method De-registers a previously registered virtual machine shutdown hook.
runFinalization(): This method runs the finalization methods of any objects pending finalization.
version(): This method returns the version of the Java Runtime Environment as a Version.
exit(int): This method terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally.
freeMemory(): This method returns the amount of free memory in the Java Virtual Machine.
totalMemory(): This method returns the total amount of memory in the Java virtual machine.
No comments:
Post a Comment