pract3
pract3
A special construct that facilitates the developers to add some code that has to be run when the Java
Virtual Machine (JVM) is shu ng down is known as the Java shutdown hook. The Java shutdown
hook comes in very handy in the cases where one needs to perform some special cleanup work
when the JVM is shu ng down. Note that handling an opera on such as invoking a special method
before the JVM terminates does not work using a general construct when the JVM is shu ng down
due to some external factors. For example, whenever a kill request is generated by the opera ng
system or due to resource is not allocated because of the lack of free memory, then in such a case, it
is not possible to invoke the procedure. The shutdown hook solves this problem comfortably by
providing an arbitrary block of code.
Taking at a surface level, learning about the shutdown hook is straigh orward. All one has to do is to
derive a class using the java.lang.Thread class, and then provide the code for the task one wants to
do in the run() method when the JVM is going to shut down. For registering the instance of the
derived class as the shutdown hook, one has to invoke the method
Run me.getRun me().addShutdownHook(Thread), whereas for removing the already registered
shutdown hook, one has to invoke the removeShutdownHook(Thread) method.
In nutshell, the shutdown hook can be used to perform cleanup resources or save the state when
JVM shuts down normally or abruptly. Performing clean resources means closing log files, sending
some alerts, or something else. So if you want to execute some code before JVM shuts down, use the
shutdown hook.
o user logoff
The addShutdownHook() method of the Run me class is used to register the thread with the Virtual
Machine.
Syntax:
The object of the Run me class can be obtained by calling the sta c factory method getRun me().
For example:
The removeShutdownHook() method of the Run me class is invoked to remove the registra on of
the already registered shutdown hooks.
Syntax:
1. public boolean removeShutdownHook(Thread hook){ }
True value is returned by the method, when the method successfully de-register the registered
hooks; otherwise returns false.
Factory method
The method that returns the instance of a class is known as factory method.
FileName: MyThread.java
4. }
5. }
6.
9.
12.
15. }
16. }
Output:
FileName: TestShutdown2.java
3.
4. Run me r=Run me.getRun me();
5.
6. r.addShutdownHook(new Thread(){
9. }
10. }
11. );
12.
15. }
16. }
Output:
The following example shows how one can use the removeShutdownHook() method to remove the
registered shutdown hook.
FileName: RemoveHookExample.java
AD
2. {
3.
6. {
7.
9. {
12. }
13.
16. {
17. try
18. {
21.
24.
27.
30. Thread.sleep(2000);
31.
34.
37. }
39. {
40. ex.printStackTrace();
41. }
42. }
43. }
Output:
Points to Remember
There are some important points to keep in mind while working with the shutdown hook.
No guarantee for the execu on of the shutdown hooks: The first and the most important thing to
keep in mind is that there is no certainty about the execu on of the shutdown hook. In some
scenarios, the shutdown hooks will not execute at all. For example, if the JVM gets crashed due to
some internal error, then there is no scope for the shutdown hooks. When the opera ng system
gives the SYSKILL signal, then also it is not possible for the shutdown hooks to come into picture.
Note that when the applica on is terminated normally the shutdown hooks are called (all threads of
the applica on is finished or terminated). Also, when the opera ng system is shut down or the user
presses the ctrl + c the shutdown hooks are invoked.
Before comple on, the shutdown hooks can be stopped forcefully: It is a special case of the above
discussed point. Whenever a shutdown hooks start to execute, one can forcefully terminate it by
shu ng down the system. In this case, the opera ng system for a specific amount of me. If the job
is not done in that frame of me, then the system has no other choice than to forcefully terminate
the running hooks.
AD
There can be more than one shutdown hooks, but there execu on order is not guaranteed: The JVM
can execute the shutdown hooks in any arbitrary order. Even concurrent execu on of the shutdown
hooks are also possible.
Within shutdown hooks, it is not allowed to unregister or register the shutdown hooks: When the
JVM ini ates the shutdown sequence, one can not remove or add more any exis ng shutdown
hooks. If one tries to do so, the IllegalStateExcep on is thrown by the JVM.
The Run me.halt() can stop the shutdown sequence that has been started: Only the Run me.halt(),
which terminates the JVM forcefully, can stop the started shutdown sequence, which also means
that invoking the System.exit() method will not work within a shutdown hook.
Security permissions are required when using shutdown hooks: If one is using the Java Security
Managers, then the Java code that is responsible for removing or adding the shutdown hooks need
to get the shutdown hooks permission at the run me. If one invokes the method without ge ng the
permission in the secure environment, then it will raise the SecurityExcep on.