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

转自:http://blog.csdn.net/dysj4099/article/details/5985596
当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();



解决方法:

view plaincopy to clipboardprint?
01.Process p = Runtime.getRuntime().exec(cmd);  
02.  
03.StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");              
04.        
05.      // kick off stderr   
06.      errorGobbler.start();  
07.        
08.      StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");  
09.      // kick off stdout   
10.      outGobbler.start();   
11.  
12.p.waitFor();  
			Process p = Runtime.getRuntime().exec(cmd);
			
			StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");            
	        
	        // kick off stderr
	        errorGobbler.start();
	        
	        StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
	        // kick off stdout
	        outGobbler.start(); 
			
			p.waitFor(); 

 


其中StreamGobbler类的代码:


view plaincopy to clipboardprint?
01.package com.sdc.callmaxent.socket;  
02.  
03.import java.io.BufferedReader;  
04.import java.io.IOException;  
05.import java.io.InputStream;  
06.import java.io.InputStreamReader;  
07.import java.io.OutputStream;  
08.import java.io.PrintWriter;  
09.  
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.}   

你可能感兴趣的:(Runtime)