Runtime.getruntime (). Addshutdownhook Add Hook

Source: Internet
Author: User

See the source, Runtime.getruntime () Addshutdownhook (Shutdownhook);

Google what it means: Add a closed hook in the JVM, and when the JVM shuts down, it executes all the hooks that have been set up in the system by means of 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.

Before you know how to close the hook, first introduce runtime.

By runtime The instance, the application is connected to its running environment. Runtime is created automatically during application startup, the application cannot create runtime, but we can get the runtime object reference of the current application through Runtime.getruntime (), through which we can obtain information about the current running environment. such as free memory, maximum memory, and add a close hook (Addshutdownhook ()) to the current virtual machine, execute the specified command (EXEC ()), and so on.

So what is called JVM shutdown? To continue Google, summarize the following:

The JVM shuts down in three ways: normal shutdown: When the last non-daemon thread ends or calls System.exit, or shuts down by means of other specific platforms (sending sigint,sigterm signals, etc.) Force shutdown: By calling the Runtime.halt method or by killing directly in the operating system (sending a sigkill signal) The JVM process is shut down: the runtime encountered a RuntimeException exception.

Close Hook (shutdown hooks)

In some cases, we need to do some cleanup work when the JVM shuts down, such as deleting the temp file, stopping the log service, and writing the memory data to disk, and the JVM provides a close hook (shutdown hooks) to do these things. It is also particularly noted that if the JVM is shut down abnormally, the child thread (the hook is essentially a child thread) will not stop. But when the JVM is forcibly shut down, these threads are forced to end.

The close hook is essentially a thread (also known as a hook thread) that listens to the JVM's shutdown. The runtime Addshutdownhook allows you to register a close hook with the JVM. The hook thread will not execute until the JVM shuts down properly, and will not execute when forced to close.

Multiple closed hooks registered in the JVM are executed concurrently, unable to guarantee the order of execution, and when all hook threads have finished executing, runfinalizersonexit for TRUE,JVM will run the Terminator first and then stop.

Note: 1.hook threads delay the shutdown time of the JVM, so minimize execution time.

2. Do not call System.exit () on the hook to close the card master JVM shutdown process. But you can call Runtime.halt ()

3. No more hooks in the hook to add and delete, will throw illegalstateexception

4. The hook added after System.exit () is not valid because the JVM is now closed.

5. When the JVM receives the Sigterm command (for example, when the operating system is closed), if the hook thread does not complete at a certain time, the hook thread may be terminated during execution.

The 6.Hook thread also throws the error, and if not captured, the hook's execution sequence is stopped.

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.