ShutdownHook

 1  public   class  App {
 2 
 3       public   static   void  main(String[] args) {
 4          System.out.println( 1 );
 5          Runtime.getRuntime().addShutdownHook( new  Thread( new  AppExitHook()));
 6          System.out.println( 2 );
 7          System.exit( 0 ); //
 8           // Runtime.getRuntime().halt(0);  // 强行终止
 9          System.out.println( 3 );
10      }
11  }
12

1  public   class  AppExitHook  implements  Runnable {
2 
3       public   void  run() {
4          System.out.println( " App exit hook running " );
5      }
6 
7  }
8 

你可能感兴趣的:(ShutdownHook)