Back-End/Java_1
[Java] Shutdown Hook
리딩리드
2016. 10. 10. 16:33
Shutdown Hook
- Shutdown hook can be used to perform cleanup resource or save the state when JVM shuts down normally or abruptly
- clean resource means closing log file, sending some alerts...]
- If you want to execute some code before JVM shuts down, use shutdown hook
1. When does the JVM shut down?
- user presses ctrl+c on the command prompt
- System.exit(int) method is invoked
- user logoff
- user shutdown etc..
2. The addShutdownHook(Thread hook) mehtod
- addShutdownHook() method of Runtime class is used to register the thread with the Virtual Machine
3. Simple Example