欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java Runtime 的 addShutdownHook 方法示例,,addShutdownH

来源: javaer 分享于  点击 11542 次 点评:90

Java Runtime 的 addShutdownHook 方法示例,,addShutdownH


addShutdownHook 方法可用来捕获进程终止时的事件,例如按了 Ctrl+C 终止应用的时候

/**  * Demonstrate adding a Shutdown Hook to the Runtime. */public class ShutDownHook {  public static void main(String[] args) {    System.err.println("Starting main program");    Runtime rt = Runtime.getRuntime();    System.err.println("Main: adding shutdown hook");    rt.addShutdownHook(new Thread() {      public void run() {        // In real life this might close a Connection or something.        System.err.println("Running my shutdown hook");      }    });    System.err.println("Main: calling Runtime.exit()");    rt.exit(0);  }}//该片段来自于http://byrx.net
相关栏目:

用户点评