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:

 PackageCom.ebways.mq.test.hook;/*** JVM hooks * Created by gmq on 2016/10/13 0013.*/ Public classJvmhook { Public Static voidMain (string[] args) {start (); //==============================1:start==========================//1. The application exits gracefully, executes specific business logic when exiting, or shuts down resources. //System.err.println ("The application is doing something");//try {//Thread.Sleep (+);//} catch (Interruptedexception e) {//e.printstacktrace ();//        }        //==============================1:end==========================//==============================2:start==========================//2, the virtual machine abnormal exit, such as the user press CTRL + C, outofmemory down, operating system shutdown and so on. Perform the necessary salvage measures at the time of exit. System.err.println ("The application is doing something"); byte[] bytes =New byte[500 * 1024 * 1024];        ThrowException (); System.err.println ("The application continues to do something"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); }        //==============================2:end==========================    }    Private Static voidthrowexception () {Throw NewOutOfMemoryError ("ASDF"); }     Public Static voidstart () {System.err.println ("The JVM is started"); Runtime.getruntime (). Addshutdownhook (NewThread () { Public voidrun () {Try {                    //Do somethingSystem.err.println ("The JVM Hook is execute!"); } Catch(Exception e) {e.printstacktrace ();    }            }        }); }}

Console information:

1, the situation

The JVM is startedthe application are doing somethingthe JVM Hook is execute!  0

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

2: The situation

 the JVM is startedthe application are doing somethingexception in thread  "main"  Java.lang.OutOfMemoryError:ASDF at Com.ebways.mq.test.hook.JVMHook.throwException ( Jvmhook.java:  41) at Com.ebways.mq.test.hook.JVMHook.main (Jvmhook.java:  28 57) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java:  43 ) at Java.lang.reflect.Method.invoke (Method.java:  606) at Com.intellij.rt.execution.application.AppMain.main (Appmain.java:  140) 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.