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

来源:互联网 发布:在线数据挖掘平台 编辑:程序博客网 时间:2024/05/17 03:01
当cmd命令执 

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



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



会造成阻塞的代码: 



view plaincopy to clipboardprint? 
01.Process p = Runtime.getRuntime().exec(cmd);  
02.  
03.p.waitFor();  
Process p = Runtime.getRuntime().exec(cmd); 

p.waitFor(); 



解决方法: 

Java代码  收藏代码
  1. view plaincopy to clipboardprint?  
  2. 01.Process p = Runtime.getRuntime().exec(cmd);    
  3. 02.    
  4. 03.StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");                
  5. 04.          
  6. 05.      // kick off stderr     
  7. 06.      errorGobbler.start();    
  8. 07.          
  9. 08.      StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");    
  10. 09.      // kick off stdout     
  11. 10.      outGobbler.start();     
  12. 11.    
  13. 12.p.waitFor();    
  14.             Process p = Runtime.getRuntime().exec(cmd);  
  15.               
  16.             StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");              
  17.               
  18.             // kick off stderr  
  19.             errorGobbler.start();  
  20.               
  21.             StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");  
  22.             // kick off stdout  
  23.             outGobbler.start();   
  24.               
  25.             p.waitFor();   
  26.   
  27.    


其中StreamGobbler类的代码: 


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