RunTime.getRunTime().addShutdownHook用法

转载: http://blog.csdn.net/wgw335363240/archive/2010/08/31/5854402.aspx
Runtime.getRuntime().addShutdownHook(shutdownHook);
   这个方法的含义说明:
    这个方法的意思就是在jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。
调用方:
Java代码 复制代码 收藏代码
  1. Runtime runtime = Runtime.getRuntime(); 
  2. EngineShutdownHook engineShutdownHook = new EngineShutdownHook(); 
  3. runtime.addShutdownHook(engineShutdownHook); 
Runtime runtime = Runtime.getRuntime();
EngineShutdownHook engineShutdownHook = new EngineShutdownHook();
runtime.addShutdownHook(engineShutdownHook);

实现:
Java代码 复制代码 收藏代码
  1. public class EngineShutdownHook extends Thread { 
  2.     private static Logger logger = Logger.getLogger(EngineShutdownHook.class.getName()); 
  3.  
  4.     public void run() { 
  5.         JediLogger.debug(logger, "EngineShutdownHook - run] Entering method."); 
  6.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss dd-MM-yyyy"); 
  7.          
  8.         Timestamp timestampStop = new Timestamp(new java.util.Date().getTime()); 
  9.         System.out.println("[INFO] " + simpleDateFormat.format(timestampStop) + " - Stopping INFO Engine..."); 
  10.         Engine.stop(); 
  11.         Timestamp timestampStopped = new Timestamp(new java.util.Date().getTime()); 
  12.         System.out.println("[INFO] " + simpleDateFormat.format(timestampStopped) + " - INFO Engine stopped."); 
  13.         
  14.         Timestamp timestampDestroy = new Timestamp(new java.util.Date().getTime()); 
  15.         System.out.println("[INFO] " + simpleDateFormat.format(timestampDestroy) + " - Destroying INFO Engine..."); 
  16.         Engine.destroy(); 
  17.          
  18.         Timestamp timestampDestroyed = new Timestamp(new java.util.Date().getTime()); 
  19.         System.out.println("[INFO] " + simpleDateFormat.format(timestampDestroyed) + " - INFO Engine destroyed."); 
  20.          
  21.         Timestamp timestampComplete = new Timestamp(new java.util.Date().getTime()); 
  22.         System.out.println("[INFO] " + simpleDateFormat.format(timestampComplete) + " - Shutdown complete"); 
  23.          
  24.         Timestamp timestampHalt = new Timestamp(new java.util.Date().getTime()); 
  25.         System.out.println("[INFO] " + simpleDateFormat.format(timestampHalt) + " - Halting JVM"); 
  26.         JediLogger.debug(logger, "com.prft.jedi.engine.EngineShutdownHook - run] Leaving method."); 
  27.     } 
public class EngineShutdownHook extends Thread {
    private static Logger logger = Logger.getLogger(EngineShutdownHook.class.getName());

    public void run() {
        JediLogger.debug(logger, "EngineShutdownHook - run] Entering method.");
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss dd-MM-yyyy");
    	
		Timestamp timestampStop = new Timestamp(new java.util.Date().getTime());
		System.out.println("[INFO] " + simpleDateFormat.format(timestampStop) + " - Stopping INFO Engine...");
        Engine.stop();
        Timestamp timestampStopped = new Timestamp(new java.util.Date().getTime());
        System.out.println("[INFO] " + simpleDateFormat.format(timestampStopped) + " - INFO Engine stopped.");
       
        Timestamp timestampDestroy = new Timestamp(new java.util.Date().getTime());
        System.out.println("[INFO] " + simpleDateFormat.format(timestampDestroy) + " - Destroying INFO Engine...");
        Engine.destroy();
        
        Timestamp timestampDestroyed = new Timestamp(new java.util.Date().getTime());
        System.out.println("[INFO] " + simpleDateFormat.format(timestampDestroyed) + " - INFO Engine destroyed.");
        
        Timestamp timestampComplete = new Timestamp(new java.util.Date().getTime());
        System.out.println("[INFO] " + simpleDateFormat.format(timestampComplete) + " - Shutdown complete");
        
        Timestamp timestampHalt = new Timestamp(new java.util.Date().getTime());
        System.out.println("[INFO] " + simpleDateFormat.format(timestampHalt) + " - Halting JVM");
        JediLogger.debug(logger, "com.prft.jedi.engine.EngineShutdownHook - run] Leaving method.");
    }
}


测试类:
Java代码 复制代码 收藏代码
  1. public class RunTimeTest { 
  2.     /**
  3.       * @param args
  4.       */ 
  5.      public static void main(String[] args) { 
  6.       Thread thread1 = new Thread() { 
  7.        public void run() { 
  8.         System.out.println("thread1..."); 
  9.        } 
  10.       }; 
  11.  
  12.       Thread thread2 = new Thread() { 
  13.        public void run() { 
  14.         System.out.println("thread2..."); 
  15.        } 
  16.       }; 
  17.  
  18.       Thread shutdownThread = new Thread() { 
  19.        public void run() { 
  20.         System.out.println("shutdownThread..."); 
  21.        } 
  22.       }; 
  23.  
  24.       Runtime.getRuntime().addShutdownHook(shutdownThread); 
  25.  
  26.       thread1.start(); 
  27.       thread2.start(); 
  28.      } 
public class RunTimeTest {
	/**
	  * @param args
	  */
	 public static void main(String[] args) {
	  Thread thread1 = new Thread() {
	   public void run() {
	    System.out.println("thread1...");
	   }
	  };

	  Thread thread2 = new Thread() {
	   public void run() {
	    System.out.println("thread2...");
	   }
	  };

	  Thread shutdownThread = new Thread() {
	   public void run() {
	    System.out.println("shutdownThread...");
	   }
	  };

	  Runtime.getRuntime().addShutdownHook(shutdownThread);

	  thread1.start();
	  thread2.start();
	 }
}

打印结果:
thread2...
thread1...
shutdownThread...

或者:
thread2...
thread1...
shutdownThread...

结论:
无论是先打印thread1还是thread2,shutdownThread 线程都是最后执行的(因为这个线程是在jvm执行关闭前才会执行)。

Java代码 复制代码 收藏代码
  1. exec(String[] cmdarray, String[] envp, File dir) 
exec(String[] cmdarray, String[] envp, File dir)

Executes the specified command and arguments in a separate process with the specified environment and working directory.

那个dir就是调用的程序的工作目录,这句其实还是很有用的。

Windows下调用程序
Java代码 复制代码 收藏代码
  1. Process proc =Runtime.getRuntime().exec("exefile"); 
Process proc =Runtime.getRuntime().exec("exefile");


Linux下调用程序就要改成下面的格式
Java代码 复制代码 收藏代码
  1. Process proc =Runtime.getRuntime().exec("./exefile"); 
Process proc =Runtime.getRuntime().exec("./exefile");


Windows下调用系统命令
Java代码 复制代码 收藏代码
  1. String [] cmd={"cmd","/C","copy exe1 exe2"}; 
  2. Process proc =Runtime.getRuntime().exec(cmd); 
String [] cmd={"cmd","/C","copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);


Linux下调用系统命令就要改成下面的格式
Java代码 复制代码 收藏代码
  1. String [] cmd={"/bin/sh","-c","ln -s exe1 exe2"}; 
  2. Process proc =Runtime.getRuntime().exec(cmd); 
String [] cmd={"/bin/sh","-c","ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);


Windows下调用系统命令并弹出命令行窗口
Java代码 复制代码 收藏代码
  1. String [] cmd={"cmd","/C","start copy exe1 exe2"}; 
  2. Process proc =Runtime.getRuntime().exec(cmd); 
String [] cmd={"cmd","/C","start copy exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);


Linux下调用系统命令并弹出终端窗口就要改成下面的格式
Java代码 复制代码 收藏代码
  1. String [] cmd={"/bin/sh","-c","xterm -e ln -s exe1 exe2"}; 
  2. Process proc =Runtime.getRuntime().exec(cmd); 
String [] cmd={"/bin/sh","-c","xterm -e ln -s exe1 exe2"};
Process proc =Runtime.getRuntime().exec(cmd);


还有要设置调用程序的工作目录就要
Java代码 复制代码 收藏代码
  1. Process proc =Runtime.getRuntime().exec("exeflie",null, new File("workpath")); 
Process proc =Runtime.getRuntime().exec("exeflie",null, new File("workpath"));

当然最好的执行系统命令的方法就是写个bat文件或是shell脚本。然后调用,那样修改和实现就简点多了。

      用Java编写应用时,有时需要在程序中调用另一个现成的可执行程序或系统命令,这时可以通过组合使用Java提供的Runtime类和Process类的方法实现。

下面是一种比较典型的程序模式:
Java代码 复制代码 收藏代码
  1. Process process = Runtime.getRuntime().exec("p.exe"); 
  2. process.waitfor( ); 
Process process = Runtime.getRuntime().exec("p.exe");
process.waitfor( );



      在上面的程序中,第一行的“p.exe”是要执行的程序名,Runtime.getRuntime()返回当前应用程序的Runtime对象,该对象的 exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。通过Process可以控制该子进程的执行或获取该子进程的信息。第二条语句的目的等待子进程完成再往下执行。 但在windows平台上,如果处理不当,有时并不能得到预期的结果。下面是笔者在实际编程中总结的几种需要注意的情况:

      1、执行DOS的内部命令 如果要执行一条DOS内部命令,有两种方法。一种方法是把命令解释器包含在exec()的参数中。

例如,执行dir命令,在 NT上,可写成exec ("cmd.exe /c dir"),在windows 95/98下,可写成“command.exe /c dir”,其中参数“/c”表示命令执行后关闭Dos立即关闭窗口。另一种方法是,把内部命令放在一个批命令my_dir.bat文件中,在Java程序中写成exec("my_dir.bat")。如果仅仅写成exec("dir"),Java虚拟机则会报运行时错误。前一种方法要保证程序的可移植性,需要在程序中读取运行的操作系统平台,以调用不同的命令解释器。后一种方法则不需要做更多的处理。
      2、打开一个不可执行的文件 打开一个不可执行的文件,但该文件存在关联的应用程序,则可以有两种方式。 以打开一个word文档a.doc文件为例,Java中可以有以下两种写法:
Java代码 复制代码 收藏代码
  1. exec("start a.doc"); 
  2. exec(" c:\\Program Files\\Microsoft Office\\office winword.exe a.doc"); 
exec("start a.doc");
exec(" c:\\Program Files\\Microsoft Office\\office winword.exe a.doc");

显然,前一种方法更为简捷方便。

      3、执行一个有标准输出的DOS可执行程序 在windows 平台上,运行被调用程序的DOS窗口在程序执行完毕后往往并不会自动关闭,从而导致Java应用程序阻塞在waitfor( )。导致该现象的一个可能的原因是,该可执行程序的标准输出比较多,而运行窗口的标准输出缓冲区不够大。解决的办法是,利用Java提供的Process 类提供的方法让Java虚拟机截获被调用程序的DOS运行窗口的标准输出,在waitfor()命令之前读出窗口的标准输出缓冲区中的内容。一段典型的程序如下:
Java代码 复制代码 收藏代码
  1. String str; 
  2. Process process = Runtime.getRuntime().exec("cmd /c dir windows"); 
  3. BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()); 
  4. while ( (str=bufferedReader.readLine()) != null) System.out.println(str);   
  5. process.waitfor(); 

你可能感兴趣的:(Java,java,addShutdownHook)