上篇博文中CallMaxentThreadPoolTask类直接使用Runtime.getRuntime().exec方法调用cmd命令,结果今天在测试时发现当cmd命令执html
行出现错误或警告时,主控程序的waitfor方法会被阻塞一直等待下去,查了查资料发现是Runtime.getRuntime().exec方法须要本身处理java
stderr 及stdout流,而解决方法便是将它们导出用别的thread处理。socket
会形成阻塞的代码:测试
- Process p = Runtime.getRuntime().exec(cmd);
-
- p.waitFor();
解决方法:this
- Process p = Runtime.getRuntime().exec(cmd);
-
- StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
-
-
- errorGobbler.start();
-
- StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
-
- outGobbler.start();
-
- p.waitFor();
其中StreamGobbler类的代码:spa
- package com.sdc.callmaxent.socket;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
-
- import com.sdc.callmaxent.util.FileUtil;
-
-
-
-
-
-
- public class StreamGobbler extends Thread {
- InputStream is;
- String type;
- OutputStream os;
-
- StreamGobbler(InputStream is, String type) {
- this(is, type, null);
- }
-
- StreamGobbler(InputStream is, String type, OutputStream redirect) {
- this.is = is;
- this.type = type;
- this.os = redirect;
- }
-
- public void run() {
- InputStreamReader isr = null;
- BufferedReader br = null;
- PrintWriter pw = null;
- try {
- if (os != null)
- pw = new PrintWriter(os);
-
- isr = new InputStreamReader(is);
- br = new BufferedReader(isr);
- String line=null;
- while ( (line = br.readLine()) != null) {
- if (pw != null)
- pw.println(line);
- System.out.println(type + ">" + line);
- }
-
- if (pw != null)
- pw.flush();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } finally{
- FileUtil.close(pw);
- FileUtil.close(br);
- FileUtil.close(isr);
- }
- }
- }
感谢http://faq.csdn.net/read/200584.html中提到的各位。.net