Java基于Runtime调用外部程序出现阻塞的解决方法

作者:shichen2014 时间:2023-11-09 04:24:23 

本文实例讲述了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程序设计的学习有所帮助。

标签:Java,阻塞
0
投稿

猜你喜欢

  • Springboot轻量级的监控组件SpringbootAdmin

    2023-08-25 10:08:31
  • java使用Filter实现自动登录的方法

    2022-09-09 15:46:47
  • TransmittableThreadLocal解决线程间上下文传递烦恼

    2023-11-09 17:09:35
  • 基于Mybatis-Plus的CRUD的实现

    2023-09-10 14:38:45
  • 手动添加jar包进Maven本地库内的方法

    2023-08-03 03:10:09
  • bs架构和cs架构的区别_动力节点Java学院整理

    2021-09-18 22:06:18
  • java Quartz定时器任务与Spring task定时的几种实现方法

    2021-11-14 22:31:47
  • idea向System.getenv()添加系统环境变量的操作

    2022-11-13 19:35:51
  • 解析c#在未出现异常情况下查看当前调用堆栈的解决方法

    2023-05-18 02:54:04
  • springboot读取自定义配置文件时出现乱码解决方案

    2022-01-29 11:09:40
  • 通过实例解析Spring Ioc项目实现过程

    2023-11-24 10:12:33
  • JavaWeb 中Cookie实现记住密码的功能示例

    2023-04-06 05:06:48
  • android AudioRecorder简单心得分享

    2021-12-13 08:23:46
  • Android实现短信验证码获取自动填写功能(详细版)

    2022-07-22 07:08:10
  • Java中的static关键字修饰属性和方法(推荐)

    2021-09-29 05:46:20
  • JAVA多线程并发下的单例模式应用

    2022-09-15 01:27:31
  • 浅谈springcloud常用依赖和配置

    2023-11-24 07:50:02
  • Android实现蒙版弹出框效果

    2023-06-14 17:13:43
  • 详解C#中的session用法

    2022-10-29 22:03:13
  • android downsample降低音频采样频率代码

    2021-11-19 15:32:59
  • asp之家 软件编程 m.aspxhome.com