Runtime.getRuntime().exec()进程阻塞问题
2015-09-07 22:01
429 查看
今天在用Runtime.getRuntime().exec()时代码走了一个或者两个小时后就会自动阻塞,网上找到了原因,是因为没有对Process的输出信息及时清理导致进程阻塞,服务失效。于是,在Runtime.getRuntime().exec()之后,p.waitFor()之前加入如下线程代码:
String cmds_ = "/usr/bin/pdf2htmlEX --no-drm 1 --split-pages 1 " + "--embed-css 0 --embed-javascript 0 --embed-image 0 --embed-font 0 --css-filename html.css " + "--fit-width 700 --bg-format jpg --auto-hint 1 --svg-node-count-limit 1 --auto-hint 1 " + "--embed-external-font 0 --dest-dir " + htmlPath + " --page-filename " + path[path.length - 1].replace(".pdf", "") + "-%d.page " + pdfPath; String[] cmds = { "/bin/sh", "-c", cmds_ }; Process pro = Runtime.getRuntime().exec(cmds); StreamGobbler errorGobbler = new StreamGobbler(pro.getErrorStream(), "Error"); StreamGobbler outputGobbler = new StreamGobbler(pro.getInputStream(), "Output"); errorGobbler.start(); outputGobbler.start(); pro.waitFor();
StreamGobbler类: <pre name="code" class="java">public class StreamGobbler extends Thread { InputStream is; String type; public StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (type.equals("Error")) { System.out.println("Error :" + line); } else { System.out.println("Debug:" + line); } } } catch (IOException ioe) { ioe.printStackTrace(); } } }
相关文章推荐
- 1.2.4 MFC中使对话框变成圆角矩形、椭圆的代码
- Android button按键点击无反应
- OpenCV 重映射
- 聚类算法分析——Kmeans算法
- UVA 1451 - Average
- 【h5-egret】js加载成第三方库
- backtrack5r3信息收集之主机综合扫描工具的使用
- 如何在点击shell/elisp类型的link时,不要弹出确认窗口
- CCMenu解析
- Coursera机器学习课程笔记(5) Neural Networks Representation
- 网页计算器
- Linux中的SSH登录
- 面试题:(1)关于fork的问题
- hihocoder1163 nim博弈
- BZOJ 1934: [Shoi2007]Vote 善意的投票 最小割
- ConcurrentModificationException异常解决
- Java Io 字符流
- Linux/Mac/Windows换行符及对'\r'和'\n'的不同处理
- Codeforces Bubble Cup 8 - Finals [Online Mirror] 解题报告
- Android学习总结(一)