Javatpoint Logo
Javatpoint Logo

JVM Shutdown Hook in Java

Developers can insert a piece of code to run when the JVM is shutting down using a specific construct called a shutdown hook. It is useful when we need to do certain cleanup procedures in the event that the JVM is shutting down.

When the VM is shutting down for reasons external to the user (such as a kill request from the operating system) or due to resource issues (such as running out of memory), handling the situation using general constructs like making sure that we call a special procedure before the application exits (invoking System.exit(0)) will not work. Shutdown hooks, which allow us to give an arbitrary code block that will be called by the JVM as it is shutting down, easily fix this problem, as we shall see in a moment.

Using a shutdown hook seems really simple at first glance. To conduct the logic we want to execute when the VM is shutting down, we need to create a class that extends the class java.lang.Thread.

Using Runtime.getRuntime() Method

Using the Runtime.getRuntime() method, one can create a shutdown hook in Java. Observe the following program.



Application Terminating ...
Shutdown Hook is running !                                                                                                        

By Extending the Thread Class

In this case, we have to add the child thread to the shutdown hook.



3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
In the clean up code
In the shutdown hook

Points To Remember Regarding Shutdown Hook

One should keep in mind the following points while working with the shutdown hook.

1. In some circumstances, shutdown hooks might not be executed!

The first thing to bear in mind is that shutdown hooks may not always execute. If the JVM collapses due to an internal fault, it can do so before even starting to process any instructions. Additionally, if the operating system sends a SIGKILL (kill -9 in Unix/Linux) or TerminateProcess (Windows) signal, the programme must end instantly without even waiting for any cleanup procedures. In addition to the aforementioned options, the Runtime.halt() method may be used to end the JVM without enabling the shutdown hooks to function.

When an application quits normally (when all threads have finished running or when System.exit(0) is invoked), shutdown hooks are activated. Additionally, when the JVM terminates as a result of outside factors like a user's request for a termination (Ctrl+C), an operating system's SIGTERM signal (a standard kill command without -9), or when the operating system terminates.

2. Shutdown Hooks can be forcefully halted before finishing up after they've been begun.

Actually, this is a particular instance of the earlier scenario. Although the hook begins running, it is still possible for it to end prematurely in circumstances like operating system shutdowns. In this scenario, after receiving the SIGTERM signal, the O/S waits for a process to end for a certain length of time. If the process doesn't end within this amount of time, the operating system (OS) issues a SIGTERM (or Windows equivalents) to forcefully end the process. So, it's feasible that this occurs midway through the shutdown hook's execution. Therefore, it is recommended that the Shutdown Hooks be carefully constructed, ensuring that they complete fast and do not result in situations like deadlocks. Additionally, the JavaDoc [1] makes it clear that one shouldn't wait for User I/O operations or conduct lengthy computations in a shutdown hook.

3. Although the execution sequence of many Shutdown Hooks cannot be guaranteed.

One can register more than one shutdown hook, as you may have inferred properly from the method name of the addShutdownHook method (instead of setShutdownHook). The JVM does not, however, guarantee the sequence in which these many hooks will be executed. Shutdown hooks may be executed by the JVM in any random sequence. Additionally, the JVM may run every one of these hooks simultaneously.

4. Shutdown Hooks cannot be registered or deregistered inside Shutdown Hooks

It is not possible to add new shutdown hooks or get rid of ones that are already in place after the JVM starts the shutdown procedure. The JVM will issue an IllegalStateException if this is tried.

5. Runtime.halt() is the only method that can end a shutdown sequence after it has begun.

Except for outside effects like SIGKILL, only Runtime.halt() (which forcibly terminates the JVM) may interrupt the execution of the shutdown sequence once it has begun. As a result, it is impossible to use System.exit() from a Shutdown Hook. In fact, if you use System.exit() inside of a shutdown hook, the VM can become stuck, in which case we might need to kill the process forcibly.

6. Security permissions are necessary to use shutdown hooks.

If Java Security Managers are being used, the code that adds or removes shutdown hooks must have runtime access to the shutdown hooks permission. SecurityException will be thrown if this method is called without authorization in a secure setting.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA