Runtime.getRuntime().exec执行阻塞问题解决 .
来源:互联网 发布:在线数据挖掘平台 编辑:程序博客网 时间:2024/05/17 03:01
当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();
解决方法:
其中StreamGobbler类的代码:
行出现错误或警告时,主控程序的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.}
0 0
- Runtime.getRuntime().exec执行阻塞问题解决
- Runtime.getRuntime().exec()执行阻塞问题解决
- Runtime.getRuntime().exec执行阻塞问题解决 .
- Runtime.getRuntime().exec()执行一连串命令阻塞解决
- Java执行Runtime.getRuntime().exec()
- Runtime.getRuntime().exec(command)的阻塞问题
- Runtime.getRuntime().exec()进程阻塞问题
- Runtime.getRuntime().exec()进程阻塞问题
- Runtime.getRuntime().exec() 输出流阻塞问题
- Android通过Runtime.getRuntime().exec实现Ping和Traceroute命令时readLine阻塞问题解决
- Android通过Runtime.getRuntime().exec实现Ping和Traceroute命令时readLine阻塞问题解决
- Runtime.getRuntime().exec()执行本地某一文件
- 用Runtime.getRuntime().exec(cmd)执行命令
- Runtime.getRuntime().exec()执行linuxshell脚本
- 执行Runtime.getRuntime().exec() 异常收集
- apk通过runtime.getruntime().exec(monkey)执行
- Runtime.getRuntime().exec执行scp失败
- Runtime.getRuntime().exec() 输出流阻塞的解决方法
- Android通用网络请求解析框架.4(代码实现,分支部分)
- 【Android基础笔记16】自定义ContentProvider
- linux中service与chkconfig的替代者systemctl
- 数字签名
- Linux查看磁盘的使用状况命令du、df
- Runtime.getRuntime().exec执行阻塞问题解决 .
- [Qt5] 减少dll依赖和大小(特别是webkit的大小和依赖) - 一 水
- 【C语言】LeetCode 3. Longest Substring Without Repeating Characters
- spring自定义标签如何实现
- spring通过注解配置
- PostgreSQL学习篇9.17 数组类型
- oracle索引
- js简单的验证码测试
- ‘8 reasons I accepted your article'