• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Shutdown Hooks API

June 20, 2012 By j2eereference Leave a Comment

What is Shutdown Hook

We know that in some situations we will have to do some clean-up operations  when the user shuts down our application. Sometimes  the user does not always follow the recommended procedure to exit the application. Java provides an API for programmers to execute code in the middle of the shutdown process,this in turn make sure that our clean-up code is always executed.

This article shows how to use a shutdown hook to guarantee that clean-up code is always run, regardless of how the user terminates the application.

Shutdown Hook is a simple thread which is registered in JVM.

It can be registered by using addShutdownHook (Thread hook). It is a method of java.lang.Runtime class

Some Important points to be remembered:

1) When JVM starts shut down process,  it starts execution of its all registered shutdown Hook  concurrently.

2) You can use Shutdown hook if you want to display messages or execute some code before JVM shut down.

3) We cannot register Shutdown Hook during shut down process. It will through IllegalStateException.

4) We cannot guaranteed that during JVM shutdown process all the registered Shutdown hook complete their execution.

5) If the VM crashes due to an error in native code then no guarantee can be made about whether or not the hooks will be run.

 

Now lets see one complete example which will demonstrate the use of shutdown hook.

Example

The below example code provides two classes

1)  ShutdownHookDemo

and

2) A subclass of Thread named ShutdownHook.

After instantiation of the public class ShutdownHookDemo , its start method is called. The start method creates a shutdown hook and registers it with the current runtime.

ShutdownHook shutdownHook = new ShutdownHook();

Runtime.getRuntime().addShutdownHook(shutdownHook);

When the user press any key, the program exits. But still the the virtual machine will run the shutdown hook and print the message “Im Shutting down” .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com. j2eereference.coreJava;
 
public class ShutdownHookDemo {
   public void start() {
     System.out.println("Im inside start");
     ShutdownHook shutdownHook = new ShutdownHook();
     Runtime.getRuntime().addShutdownHook(shutdownHook);  //Registering in the current runtime.
  }
 
public static void main(String[] args) {
  ShutdownHookDemo shutdownHookDemo = new ShutdownHookDemo();
  shutdownHookDemo.start();
  try {
   System.in.read();
   }
  catch(Exception e) {
   }
  }
}

Now lets a create class which actually does the job to be done while VM is shutting down

1
2
3
4
5
6
7
package com. j2eereference.coreJava;
 
class ShutdownHook extends Thread {
  public void run() {
   System.out.println("Im Shutting down");
}
}

Output

Im inside start

Im Shutting down

When finalizers will run, before, during, or after shutdown hooks?

Finalization-on-exit processing is done after all shutdown hooks have finished. Otherwise a hook may fail if some live objects are finalized prematurely.

 

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.