java wait() for if_关于JAVA Project.waitfor()死锁问题

在正常情况下我们可以用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){}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值