Close the hook through JDK source code analysis

shutdown hook

When users close the application, they need to clean up the aftermath, but the problem is that some users will not close the application according to the recommended method, which may make the aftermath impossible. Like tomcat, call the start method of server to start the container, and then call start level by level. When the shutdown command is issued, the shutdown function will be started, but the shutdown may occur unexpectedly, resulting in the application not entering the shutdown method we have formulated. How to solve this problem so that even if there is an accident, it can normally enter the shutdown process.

Fortunately, Java provides an elegant way to solve this problem. Make the closed aftercare code executable. Java's close hook ensures that it is always executed, no matter how the user terminates the application. Unless the user kills, this is a dead hole.

Java provides a shutdown hook mechanism, which allows us to do some clearing work when the program exits normally or exceptions occur. The method used is also very simple, Java Runtime. Add shutdown hook (thread hook). The close hook can actually be regarded as a thread that has been initialized but has not been started. When the JVM is closed, all registered close hooks will be executed concurrently.

For Java, virtual opportunity closes the following operations:

(1) System call system Exit() method

(2) When the last daemon thread of the program exits, the application exits normally.

(3) The user forcibly interrupts the program, such as Ctrl + C and other methods to interrupt the Java program

Turn off hook generation:

1. Create subclass of thread

2. Implement the run method, which will be called when the application closes, and there is no need to call the start method

3. Instantiate the close hook class in the application

4. Use runtime registration to close the hook

Hook execution timing

After registering with the JVM, when will the hook be called and when will it not be called? It can be divided into the following situations:

Add delete hook

Hooks are added and deleted through the runtime, and the implementation is relatively simple. You can see that addshutdownhook and removeshutdownhook methods first check whether they have the authority of shutdownhooks through the security manager, and then add and delete hooks through applicationsshutdownhooks.

Applicationshutdownhooks safekeeping hook

Application shutdown hooks can be regarded as a container for keeping all closed hooks, mainly through an identity HashMap

With the hooks variable, adding and deleting hooks is to directly perform put and remove operations on the HashMap. Some checks will also be made before the operation. For example, three judgments will be made before adding hooks:

1. Whether all hooks have started to execute. If hooks is null, it means that all closed hooks have started to execute. At this time, they can't be added.

2. Whether the hook status is alive. If yes, it means that the hook is already running and cannot be added.

3. Whether the hook has been included. If it has been included, it cannot be added.

Similar judgment logic includes the remove operation.

The runhooks method is responsible for the task of starting all hooks in applicationshutdownhooks. Its logic is as follows:

1. Lock the applicationshutdownhooks class and get all hooks, and then set the hooks variable to null.

2. Traverse all hooks and start hooks respectively. As mentioned earlier, closing hooks can actually be regarded as a thread that has been initialized but has not been started. Here, call the start method to start it.

3. Use the join method to coordinate all hook threads and wait for them to complete execution.

Who is responsible for calling the runhooks method of applicationshutdownhooks? As follows, it actually becomes a runnable object and is added to the shutdown class. The run method of runnable is responsible for calling the runhooks method. Next, it depends on when the shutdown class executes the runnable object.

Hook in shutdown

The logic of adding the runnable object of applicationshutdownhooks to shutdown is as follows,

Slot indicates the element in the hooks array to which the runnable object is assigned. There is also a hooks variable in shutdown, which is of runnable [] type and has a length of max_ SYstem_ Hooks, i.e. 10. This array can be regarded as the priority implementation of the hook. The array subscript is used to indicate the priority, and slot = 1 indicates that it is assigned to the second element in the array.

Registershutdowninprogress indicates whether to allow hook registration, even if shutdown is being performed. Pass in false before, which is obviously not allowed. The state > running condition indicates that exceptions should be thrown in other states, except the running state. This is well understood. There are three states, running, hooks and finalizers, with values of 0, 1 and 2 respectively. If registershutdowninprogress is true, as long as it is not in the finalizers state, and the slot must be greater than the subscript of the current hook array.

In the case of hook execution timing mentioned earlier, the JVM will call the sequence method of shutdown class, as follows:,

First, judge that the current state is not equal to hooks, then return directly, and then execute the runhooks method, which is also our main method. Then set the status to finalizers, and finally call the runallfinalizers method to execute all finalizers if necessary. Therefore, the runhooks method will be called when the JVM is shut down.

The logic of the runhooks method is simple. It is to traverse the runnable array and call its run methods one by one for execution.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>