Runtime.getRuntime().exec执行阻塞问题解决

上篇博文中CallMaxentThreadPoolTask类直接使用Runtime.getRuntime().exec方法调用cmd命令,结果今天在测试时发现当cmd命令执

行出现错误或警告时,主控程序的waitfor方法会被阻塞一直等待下去,查了查资料发现是Runtime.getRuntime().exec方法需要自己处理

stderr 及stdout流,而解决方法即是将它们导出用别的thread处理。

会造成阻塞的代码:

  1. Process p = Runtime.getRuntime().exec(cmd);  
  2.   
  3. p.waitFor();  

解决方法:

  1. Process p = Runtime.getRuntime().exec(cmd);  
  2.   
  3. StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");              
  4.         
  5.       // kick off stderr  
  6.       errorGobbler.start();  
  7.         
  8.       StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");  
  9.       // kick off stdout  
  10.       outGobbler.start();   
  11.   
  12. p.waitFor();  

其中StreamGobbler类的代码:

  1. package com.sdc.callmaxent.socket;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStream;  
  8. import java.io.PrintWriter;  
  9.   
  10. import com.sdc.callmaxent.util.FileUtil;  
  11.   
  12. /** 
  13.  * 用于处理Runtime.getRuntime().exec产生的错误流及输出流 
  14.  * @author shaojing 
  15.  * 
  16.  */  
  17. public class StreamGobbler extends Thread {  
  18.     InputStream is;  
  19.     String type;  
  20.     OutputStream os;  
  21.           
  22.     StreamGobbler(InputStream is, String type) {  
  23.         this(is, type, null);  
  24.     }  
  25.   
  26.     StreamGobbler(InputStream is, String type, OutputStream redirect) {  
  27.         this.is = is;  
  28.         this.type = type;  
  29.         this.os = redirect;  
  30.     }  
  31.       
  32.     public void run() {  
  33.         InputStreamReader isr = null;  
  34.         BufferedReader br = null;  
  35.         PrintWriter pw = null;  
  36.         try {  
  37.             if (os != null)  
  38.                 pw = new PrintWriter(os);  
  39.                   
  40.             isr = new InputStreamReader(is);  
  41.             br = new BufferedReader(isr);  
  42.             String line=null;  
  43.             while ( (line = br.readLine()) != null) {  
  44.                 if (pw != null)  
  45.                     pw.println(line);  
  46.                 System.out.println(type + ">" + line);      
  47.             }  
  48.               
  49.             if (pw != null)  
  50.                 pw.flush();  
  51.         } catch (IOException ioe) {  
  52.             ioe.printStackTrace();    
  53.         } finally{  
  54.             FileUtil.close(pw);  
  55.             FileUtil.close(br);  
  56.             FileUtil.close(isr);  
  57.         }  
  58.     }  
  59. }   

感谢http://faq.csdn.net/read/200584.html中提到的各位。

原文地址:https://www.cnblogs.com/xin36933/p/3554736.html