In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to add exit event response to Java applications, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
A complete Java application usually has at least one end point of the application. For general programs, according to their needs and personal preferences, system developers will end the program by adding System.exit (0) or System.out (- 1) at the end of the program, or without these instructions, let the program run to the end naturally.
For example, the following typical code
Package untitled14
/ * *
* This application is to demo how an applcation end
* /
Public class Test {
Public Test () {}
Public static void main (String [] args) {
Test test1 = new Test ()
/ /.
System.out.println ("hello world")
/ / Do something before system exit
System.exit (0); / / or you don't have to write this code and let the program end naturally.
}
}
For simple applications, we can directly add the work that needs to be done before the application exits before the System.exit (0) code is executed, such as closing the network connection, closing the database connection and so on.
However, for more complex multithreaded applications, the running state of threads is more complex, it is difficult for us to predict when the program will end, and how can we deal with the work we need to do when the application end event arrives? This uses the Java event-out handling mechanism for the exit of the application.
For the acquisition of the current application object, Java uses the Runtime static method: Runtime.getRuntime () registers a shutdown hook event to the Java virtual machine through Runtime's void addShutdownHook (Thread hook) method, so that once the program end event arrives, we run thread hook. In practical application, as long as some of the work that the program needs to do before it is done is done directly through the thread hook. The specific demonstration code is as follows:
/ *
This program only demonstrates how to add a system exit event handling mechanism to a Java application
* * /
Package untitled14
Import java.util.*
Import java.io.*
/ * *
* This application is used to demo how to hook the event of an application
* /
Public class Untitled1 {
Public Untitled1 () {
DoShutDownWork ()
}
/ *
* This is the right work that will do before the system shutdown
* for demonstration purposes, an event handler has been added for the exit of the application
* when the application exits, the date of the program exit is written to the d: .log file
* * /
Private void doShutDownWork () {
Runtime.getRuntime () .addShutdownHook (new Thread () {
Public void run () {
Try {
FileWriter fw = new FileWriter ("d: .log")
System.out.println ("Im going to end")
Fw.write ("the application ended!" + (new Date ()) .toString ())
Fw.close ()
}
Catch (IOException ex) {
}
}
})
}
/ *
* this is the entrance to the program. For demonstration purposes only, the code in the method does not matter.
* * /
Public static void main (String [] args) {
Untitled1 untitled11 = new Untitled1 ()
Long s = System.currentTimeMillis ()
For (int I = 0; I < 100000000; iTunes +) {
/ / add the code you need to handle here
}
Long se = System.currentTimeMillis ()
System.out.println (se-s)
}
}
In the above program, we can see that by adding Runtime.getRuntime (). AddShutdownHook (new Thread ()) event listening to the program, capture the arrival of the system exit message, and then do what we need to do to make our program more robust!
The above is all the content of the article "how to add exit event responses to Java applications". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.