When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I need to shutdown my application. Could anyone please explain me this by giving some example.
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As far as I know, I will explain this below. You can google it and find lot of information too.
addShutdownHook()will register some actions which is to be performed on a Program’s termination. The program that you start ends in two ways:If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:
will print a
Running Shutdown Hookat the time of program termination at any point. You might even call aSystem.exit(0).For examples, you can google, there are enough of them. And the question ‘When should you use this’ is like asking ‘What does
catchdo in atry-catchstatement’.You might have many situations like:
All this will be needed for some point of time.
For examples you can go in here Example 1 or Example 2