Java Process.exitValue & Process.waitFor()

http://gohands.blogbus.com/logs/172834178.html

Process.exitValue() 采用非阻塞的方式返回,如果没有立即拿到返回值,则抛出异常

Process.waitFor() 当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。但是如果我们在调用此方法时,如果不注意的话,很容易出现主线程阻塞,Process也挂起的情况。在调用waitFor() 的时候,Process需要向主线程汇报运行状况,所以要注意清空缓存区,即InputStream和ErrorStream,在网上,很多只提到处理 InputStream,忽略了ErrorStream。以下一段代码,贴出来,仅做参考。

  1. import  java.io.BufferedReader;  
  2. import  java.io.IOException;  
  3. import  java.io.InputStream;  
  4. import  java.io.InputStreamReader;  
  5. import  java.util.List;  
  6.   
  7. public   class  Temp  implements  Runnable {  
  8.   
  9.     public   void  run() {  
  10.   
  11.         String PATH_TO_SWF = "D:\\Program Files\\SWFTools\\pdf2swf.exe" ;  
  12.         String fromFilename = "G:\\test\\read\\suanfa.pdf" ;  
  13.         String saveFilename = "G:\\test\\read\\xiaoxiaoxxx.swf" ;  
  14.   
  15.         List command = new  java.util.ArrayList();  
  16.         command.add(PATH_TO_SWF);  
  17.         command.add("-o" );  
  18.         command.add(saveFilename);  
  19.         command.add("-t" );  
  20.         command.add(fromFilename);  
  21.         command.add("-s" );  
  22.         command.add("languagedir="  +  "G:\\test\\read\\xpdf\\chinese-simplified" );  
  23.   
  24.         ProcessBuilder builder = new  ProcessBuilder();  
  25.         builder.command(command);  
  26.   
  27.         try  {  
  28.             Process convertImgProcess = builder.start();  
  29.             final  InputStream is1 = convertImgProcess.getInputStream();  
  30.             final  InputStream is2 = convertImgProcess.getErrorStream();  
  31.             new  Thread() {  
  32.                 public   void  run() {  
  33.                     BufferedReader br = new  BufferedReader(  
  34.                             new  InputStreamReader(is1));  
  35.                     try  {  
  36.                         String lineB = null ;  
  37.   
  38.                         while  ((lineB = br.readLine()) !=  null ) {  
  39.                             if  (lineB !=  null ) System.out.println(lineB);  
  40.   
  41.                         }  
  42.                     } catch  (IOException e) {  
  43.                         e.printStackTrace();  
  44.                     }  
  45.                 }  
  46.             }.start();  
  47.   
  48.             new  Thread() {  
  49.                 public   void  run() {  
  50.   
  51.                     BufferedReader br2 = new  BufferedReader(  
  52.                             new  InputStreamReader(is2));  
  53.                     try  {  
  54.   
  55.                         String lineC = null ;  
  56.                         while  ((lineC = br2.readLine()) !=  null ) {  
  57.   
  58.                             if  (lineC !=  null ) System.out.println(lineC);  
  59.                         }  
  60.                     } catch  (IOException e) {  
  61.                         e.printStackTrace();  
  62.                     }  
  63.                 }  
  64.             }.start();  
  65.             convertImgProcess.waitFor();  
  66.         } catch  (Exception e) {  
  67.             System.err.println(e);  
  68.         }  
  69.     }  

 

你可能感兴趣的:(Java Process.exitValue & Process.waitFor())