在正常情况下我们可以用Project.waitfor()的返回值是否等于0的方法来判断java调用外部程序是Pass或者是Fail。

但是这个方法往往会被因进程堵塞而导致程序发生死锁,无法再继续执行外部程序。

因为本地的系统对标准输入和输出所提供的缓冲池有限,所以错误的对标准输出快速的写入和从标准输入快速的读入都有可能造成子进程死锁。问题的关键在缓冲区这个地方:可执行程序的标准输出比较多,而运行窗口的标准缓冲区不够大,所以发生阻塞。接着来分析缓冲区,当Runtime对象调用exec(cmd)后,JVM会启动一个子进程,该进程会与JVM进程建立三个管道连接:标准输入,标准输出和标准错误流。假设该程序不断在向标准输出流和标准错误流写数据,而JVM不读取的话,当缓冲区满之后将无法继续写入数据,最终造成阻塞在waitfor()这里。

但是在其中过程中真正起关键作用的缓冲区是getErrorStream()对应的那个缓冲区没有被清空,意思就是说其实只要及时读取标准错误流缓冲区的数据程序就不会被block。

原先的代码

//run bat file
Process project = Runtime.getRuntime().exec("cmd.exe /c " + batpath.replaceAll(" ", "\" \""));
int exitcode=project.waitFor();
//kill the process
project.destroy();
logger.info(exitcode);
//if the exitcode is 0, the RIA TEST is passed,else the RIA TEST is failed
if(exitcode==0){
logger.info("============ is Passed============");
}
else{
Boolean resultFlag=false;
logger.info("============ is Failed============");
Assert.assertTrue(bugID+"Failed",resultFlag);
}

修改后的代码:

增加两个线程来读取标准输出流和标准错误流

		try{
//run bat file
Process project = Runtime.getRuntime().exec("cmd.exe /c " + batpath.replaceAll(" ", "\" \""));
final InputStream br = project.getInputStream();
final InputStream br_error = project.getErrorStream();
//run 2 threads to read the standard InputStream and the ErrorStream to avoid the project.
//waitfor()method blocked
new Thread() {
public void run() {
BufferedReader br1 = new BufferedReader(new InputStreamReader(br));
try {
String line1 = null;
while ((line1 = br1.readLine()) != null) {
if (line1 != null){
logger.info("RIATest info: "+line1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
//run thread to read the standard ErrorStream
new Thread() {
public void run() {
BufferedReader br2 = new BufferedReader(new InputStreamReader(br_error));
try {
String line2 = null;
while ((line2 = br2.readLine()) != null) {
if (line2 != null){
logger.info("Error: "+line2);
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
br_error.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start(); try{ int exitcode=project.waitFor();
//kill the process
project.destroy();
logger.info(exitcode);
//if the exitcode is 0, the RIA TEST is passed,else the RIA TEST is failed
if(exitcode==0){
logger.info("============ is Passed============");
}
else{
Boolean resultFlag=false;
logger.info("============ is Failed============");
Assert.assertTrue(bugID+"Failed",resultFlag);
} }
catch(Throwable e){ e.printStackTrace(); } }
catch(IOException e){
e.printStackTrace();
try
{
project.getErrorStream().close();
project.getInputStream().close();
project.getOutputStream().close();
}
catch(Exception ee){}
}

关于JAVA Project.waitfor()死锁问题的更多相关文章

  1. java 代码执行cmd 返回值异常 (关于JAVA Project.waitfor()返回值是1)

    关于JAVA Project.waitfor()返回值是1   0条评论 Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project ...

  2. 关于JAVA Project.waitfor()返回值是1

    Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project=null.而去根目录下点击被调用的bat文件发现也可以被正确执行. 这时 ...

  3. java compiler level does not match the version of the installed java project facet 解决方案

    项目出现 java compiler level does not match the version of the installed java project facet 错误,一般是项目移植出现 ...

  4. java compiler level does not match the version of the installed java project facet

    Java compiler level does not match the version of the installed java project facet错误的解决 因工作的关系,Eclip ...

  5. export a java project to runable jar

    When a java project needs to be transfered to another machine, e.g. vps, we need to export it to a r ...

  6. Using Maven to generate a Java Project or Web project

    I often to generate a Java project or Web project with Eclipse tool. Well, I have no idea when I wan ...

  7. Java compiler level does not match the version of the installed Java project facet.(转)

    Java compiler level does not match解决方法 从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description  Resource P ...

  8. maven项目 Java compiler level does not match the version of the installed Java project facet

    因工作的关系,Eclipse开发的Java项目拷来拷去,有时候会报一个很奇怪的错误.明明源码一模一样,为什么项目复制到另一台机器上,就会报“java compiler level does not m ...

  9. Java compiler level does not match the version of the installed Java project facet.问题

    从同事那里拷贝过来的web项目,导入到eclipse中,出现Java compiler level does not match the version of the installed Java p ...

随机推荐

  1. 第1章 软件测试基本概念(Week1,3月3日)

    一.对软件的认识 1. 什么是软件 2. 软件的分类 3. 软件开发的生命周期模型 (1)瀑布模型 (2)Scrum 其实对用瀑布模型这种臃肿不堪.要求严格.而无法适应软件开发周期变化的开发模型,渐渐 ...

  2. PL/SQL(二):变量

    变量 标识符定义 PL/SQL程序设计中的标识符定义与SQL的标识符定义的要求相同.要求和限制有: 个字符. )首字符必须为字母. )不区分大小写. )不能使用SQL保留字. )对标识符的命名最好遵循 ...

  3. Android KeyCode(官方)

    Constants public static final int ACTION_DOWN Added in API level 1 getAction() value: the key has be ...

  4. MapReduce调度与执行原理之作业提交

    前言 :本文旨在理清在Hadoop中一个MapReduce作业(Job)在提交到框架后的整个生命周期过程,权作总结和日后参考,如有问题,请不吝赐教.本文不涉及Hadoop的架构设计,如有兴趣请参考相关 ...

  5. CSDN头版头条 《近匠》 Wijmo 5 CTO:从Web到移动,我的25年编程生涯

    现年52岁的Bernardo Castilho先生是GrapeCity(中文名为葡萄城)ComponentOne公司的CTO,在与他的对话过程中.充满风趣严谨和厚重的历史感. 当作为年轻人的我们崇拜着 ...

  6. SVM(支持向量机)(一)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) SVM(Support Vector Machines)系列会循序 ...

  7. python 在 eclipse 上的编码配置问题

    Eclipse的设置 window->preferences->general->editors->text editors->spelling->encoding ...

  8. No matching code signing identity found

    真机调试过程中弹出这个问题,网上找到的解决的方法,记录一下. .... 弄完这些步骤之后,上面多出一个 IOS disturbution.所以出现这个问题的解决办法应该是设置的证书没有刷新到本地所致.

  9. UVA 10795 - A Different Task(递归)

     A Different Task  The (Three peg) Tower of Hanoi problem is a popular one in computer science. Brie ...

  10. php session 管理

    function do_login(){ //获取用户名和密码信息,和数据库中比对 echo 111111111; dump($_POST); dump($_SESSION); echo 222222 ...