Runtime.getRuntime().exec()出现阻塞,导致后续程序无法运行!

来源:互联网 发布:java thread.sleep用法 编辑:程序博客网 时间:2024/05/16 18:37

我是参考这位仁兄做的。http://www.cnblogs.com/yejg1212/archive/2013/06/02/3114242.html 解决阻塞.


@SuppressWarnings("unchecked")

public class ExeSh {
// 保存进程的输入流信息
    private List<String> stdoutList = new ArrayList<String>();
    // 保存进程的错误流信息
    private List<String> erroroutList = new ArrayList<String>();


    public void executeCommand(String command) {
        // 先清空
        stdoutList.clear();
        erroroutList.clear();
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(command);


            // 创建2个线程,分别读取输入流缓冲区和错误流缓冲区
            ThreadUtil stdoutUtil = new ThreadUtil(p.getInputStream(), stdoutList);
            ThreadUtil erroroutUtil = new ThreadUtil(p.getErrorStream(), erroroutList);
            //启动线程读取缓冲区数据
            stdoutUtil.start();
            erroroutUtil.start();


            p.waitFor();          
            LogUtil.printlnStr("启动完毕.");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public List<String> getStdoutList() {
        return stdoutList;
    }
    public List<String> getErroroutList() {
        return erroroutList;
    }
class ThreadUtil implements Runnable {
    // 设置读取的字符编码
    private String character = "GB2312";
    private List<String> list;
    private InputStream inputStream;
    public ThreadUtil(InputStream inputStream, List<String> list) {
        this.inputStream = inputStream;
        this.list = list;
    }
    public void start() {
        Thread thread = new Thread(this);
        thread.setDaemon(true);//将其设置为守护线程
        thread.start();
    }


    public void run() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream, character));
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line != null) {
                    list.add(line);
                }
            }
        } catch (IOException e) {          
            LogUtil.pSTS(e);
        } finally {
            try {
                //释放资源
                inputStream.close();
                br.close();
            } catch (IOException e) {    
                LogUtil.pSTS(e);
            }
        }
    }
}
/**
* java 调用shell脚本
* @param dataSetMap
* @param parameter
* @return Object
* @throws Exception
*/


public Object runShell(Map dataSetMap, Object parameter) {
Map<String, String> maps = new HashMap<String, String>();
DataSet calldataset = (DataSet) dataSetMap.get("calldataset");
Collection<FcznShellModel> pc = calldataset.getRecords();
String sh = pc.iterator().next().getSh();

ExeSh util = new ExeSh();
util.executeCommand("/bin/sh "+sh);
printList(util.getStdoutList());

printList(util.getErroroutList());
maps.put("system", "执行完成。。。" + sh);

return maps;
}
public static void printList(List<String> list){
    for (String string : list) {     
        LogUtil.printlnStr(string);
    }
}
}
0 0
原创粉丝点击