Runtime.getRuntime().exec()进程阻塞问题

来源:互联网 发布:金税盘开票软件最新版 编辑:程序博客网 时间:2024/04/30 02:07

今天在用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();          }      }  }  



0 0
原创粉丝点击