惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
H
Hacker News: Front Page
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
T
Tenable Blog
博客园 - 叶小钗
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
量子位
P
Proofpoint News Feed
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
S
Schneier on Security
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
G
GRAHAM CLULEY
G
Google Developers Blog
月光博客
月光博客
V
V2EX
T
Troy Hunt's Blog
A
Arctic Wolf

博客园 - xin36933

浅析android OCR文字识别 Android中使用代码截图的各种方法总结 Android通过tcpdump抓包 Android 开机自动启动服务 SqlServer表死锁的解决方法 数据库 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。 修改Android工程版本 android利用剪切板来实现数据的传递 ANDROID开发之SQLite详解 浅析SQLite数据库开发常用管理工具 android DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹的解决办法 Android手机获取手机唯一识别号(转) android 程序时时获取logcat信息 Android adb shell启动应用程序的方法 android打电话、发短信实现 Android全局变量的定义与使用 安卓4.0/4.1/4.2手机怎么打开USB调试模式 Android中的DDMS进行调试 GC_EXTERNAL_ALLOC freed 与 GC_EXPLICIT freed 是什么?
Runtime.getRuntime().exec执行阻塞问题解决
xin36933 · 2014-02-18 · via 博客园 - xin36933

上篇博文中CallMaxentThreadPoolTask类直接使用Runtime.getRuntime().exec方法调用cmd命令,结果今天在测试时发现当cmd命令执

行出现错误或警告时,主控程序的waitfor方法会被阻塞一直等待下去,查了查资料发现是Runtime.getRuntime().exec方法需要自己处理

stderr 及stdout流,而解决方法即是将它们导出用别的thread处理。

会造成阻塞的代码:

  1. Process p = Runtime.getRuntime().exec(cmd);  
  2.   
  3. p.waitFor();  

解决方法:

  1. Process p = Runtime.getRuntime().exec(cmd);  
  2.   
  3. StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");              
  4.         
  5.         
  6.       errorGobbler.start();  
  7.         
  8.       StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");  
  9.         
  10.       outGobbler.start();   
  11.   
  12. p.waitFor();  

其中StreamGobbler类的代码:

  1. package com.sdc.callmaxent.socket;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.OutputStream;  
  8. import java.io.PrintWriter;  
  9.   
  10. import com.sdc.callmaxent.util.FileUtil;  
  11.   
  12.  
  13.  
  14.  
  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. }   

感谢http://faq.csdn.net/read/200584.html中提到的各位。