In the Java window
ProgramPress Ctrl + C to forcibly stop the Java program. Clicking the close button in the window also forces the program to stop.
Runtime. getruntime (). addshutdownhook (hooker) allows you to register a thread and execute it after system. Exit () and before finalize is called.
According to the document, if multiple programs have registered hooker, they will be enabled at the same time, and the order is not guaranteed. For example, if you want to send messages through JMS before closing, it is very likely that the JMS service has been closed.
Google tells us that there is another good way to use sun's exclusive secret technology to directly intercept system signals. Note: It is invalid on non-Sun JVM.
Web: http://www.smotricz.com/kabutz/Issue043.html
Simple application:
Signalhandler handler = new signalhandler (){
Public void handle (signal sig ){
Server. This. Exit (0 );
}
};
Signal. Handle (new signal ("int"), Handler); // hook Ctrl + c
Signal. Handle (new signal ("term"), Handler); // hook Alt + F4
OK. Now the program can be closed for limited processing ~