示例如下:
package shutdownHookDemo; public class Demo { public static void main(String[] args) { System.out.println("hello world"); Runtime.getRuntime().addShutdownHook(new Thread(){ @Override public void run() { System.out.println("程序退出了"); } }); } }
打印结果如下:
hello world
程序退出了
addShutdownHook API的作用:
Runtime.getRuntime().addShutdownHook()
原文:https://www.cnblogs.com/silenceshining/p/14965172.html