Closure Hooks for Java (Shutdown hook)

Source: Internet
Author: User

Runtime.getruntime (). Addshutdownhook (Shutdownhook);

The implication of this method is that the method means adding a closed hook to the JVM, and when the JVM shuts down, it executes all the hooks that have been set in the system via the method Addshutdownhook, and the JVM shuts down when the system finishes executing the hooks. So these hooks can be used for memory cleanup, object destruction and so on when the JVM shuts down.

Use

1 The application exits gracefully, executes specific business logic when exiting, or shuts down resources.

2 Abnormal exit of virtual machine, such as user press CTRL + C, outofmemory down, operating system shutdown, etc. Perform the necessary salvage measures at the time of exit.

Example:

public class Jvmhook {

 public static void Start () {
  system.out.println ("The JVM is Started ");
  runtime.getruntime (). Addshutdownhook (New Thread () {
   public void Run () {
    try{
     //do something
      System.out.println ("The JVM Hook is execute");
    }catch (Exception e) {
     e.printstacktrace ();
    }
   }
  });
 }
 
 public static void Main (string[] args) {
  start ();
  
  system.out.println ("The application is doing something");
  
  
  
  try {
   thread.sleep (+);
  } catch (Interruptedexception e) {
   e.printstacktrace ();
  }
 }
}

Output Result:

The JVM is started
The application is doing something
The JVM Hook is execute

The last one is output after three seconds when the JVM shuts down.

examples given for the 2nd use:Package Com.java.seven;Public class Jvmhook {public static void Start () {
System.out.println ("The JVM is started");
Runtime.getruntime (). Addshutdownhook (New Thread () {
public void Run () {
try{
Do something
System.out.println ("The JVM Hook is execute");
}catch (Exception e) {
E.printstacktrace ();
}
}
});
}

public static void Main (string[] args) {
Start ();

System.out.println ("The application is doing something");

Byte[] B = new byte[500*1024*1024];

System.out.println ("The application continues to do something");

try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}

Output Result:

The JVM is started
The application is doing something
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
At Com.java.seven.JVMHook.main (jvmhook.java:24)
The JVM Hook is execute

There are some remedial measures that can be made when outofmemoryerror.

Recommendation: The same JVM is best to use only one closure hook, instead of each service using a different closure hook, using multiple close hooks may appear the current hook to rely on the service may have been closed by another closure hook. To avoid this situation, it is recommended that the shutdown operation be executed serially in a single thread, thus avoiding race conditions or deadlocks between shutdown operations.

Closure Hooks for Java (Shutdown hook)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.