java runtime.exec 阻塞_Java基于Runtime调用外部程序出现阻塞的解决方法

本文实例讲述了Java基于Runtime调用外部程序出现阻塞的解决方法, 是一个很实用的技巧。分享给大家供大家参考。具体分析如下:

有时候在java代码中会调用一些外部程序,比如SwfTools来转换swf、ffmpeg来转换视频等。如果你的代码这样写:Runtime.getRuntime().exec(command),会发现程序一下就执行完毕,而在命令行里要执行一会,是因为java没有等待外部程序的执行完毕,此时就需要使用阻塞,来等待外部程序执行结果:

InputStream stderr = process.getInputStream();

InputStreamReader isr = new InputStreamReader(stderr, "GBK");

BufferedReader br = new BufferedReader(isr);

String line = null;

while ((line = br.readLine()) != null)

System.out.println(line);

int exitValue = process.waitFor();

对于一般的外部程序使用上面的阻塞代码就可以,至少pdf2swf.exe是没有问题的。

但是紧接着又发现对于ffmpeg来说,以上代码会让程序卡住不动,需要使用另一种方式,封装成了一个方法,如下:

@SuppressWarnings("static-access")

public static int doWaitFor(Process process) {

InputStream in = null;

InputStream err = null;

int exitValue = -1; // returned to caller when p is finished

try {

in = process.getInputStream();

err = process.getErrorStream();

boolean finished = false; // Set to true when p is finished

while (!finished) {

try {

while (in.available() > 0) {

// Print the output of our system call

Character c = new Character((char) in.read());

System.out.print(c);

}

while (err.available() > 0) {

// Print the output of our system call

Character c = new Character((char) err.read());

System.out.print(c);

}

// Ask the process for its exitValue. If the process

// is not finished, an IllegalThreadStateException

// is thrown. If it is finished, we fall through and

// the variable finished is set to true.

exitValue = process.exitValue();

finished = true;

} catch (IllegalThreadStateException e) {

// Process is not finished yet;

// Sleep a little to save on CPU cycles

Thread.currentThread().sleep(500);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (in != null) {

in.close();

}

} catch (IOException e) {

e.printStackTrace();

}

if (err != null) {

try {

err.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return exitValue;

}

希望本文所述对大家Java程序设计的学习有所帮助。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值