- 浏览: 103108 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
qiushuzhao:
';lll
spring data redis源码框架分析 -
qiushuzhao:
[color=yellow][/color]lll
spring data redis源码框架分析 -
jiuyuehe:
博主你好,用了你方法以后果然没有阻塞了,但是原理还是不太明白, ...
[转载]Java Process.exitValue & Process.waitFor()
[转载]Java Process.exitValue & Process.waitFor()
- 博客分类:
- Java
Process.exitValue() 采用非阻塞的方式返回,如果没有立即拿到返回值,则抛出异常
Process.waitFor() 当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。但是如果我们在调用此方法时,如果不注意的话,很容易出现主线程阻塞,Process也挂起的情况。在调用waitFor() 的时候,Process需要向主线程汇报运行状况,所以要注意清空缓存区,即InputStream和ErrorStream,在网上,很多只提到处理InputStream,忽略了ErrorStream。以下一段代码,贴出来,仅做参考。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; public class Temp implements Runnable { public void run() { String PATH_TO_SWF = "D:\\Program Files\\SWFTools\\pdf2swf.exe"; String fromFilename = "G:\\test\\read\\suanfa.pdf"; String saveFilename = "G:\\test\\read\\xiaoxiaoxxx.swf"; List<String> command = new java.util.ArrayList<String>(); command.add(PATH_TO_SWF); command.add("-o"); command.add(saveFilename); command.add("-t"); command.add(fromFilename); command.add("-s"); command.add("languagedir=" + "G:\\test\\read\\xpdf\\chinese-simplified"); ProcessBuilder builder = new ProcessBuilder(); builder.command(command); try { Process convertImgProcess = builder.start(); final InputStream is1 = convertImgProcess.getInputStream(); final InputStream is2 = convertImgProcess.getErrorStream(); new Thread() { public void run() { BufferedReader br = new BufferedReader( new InputStreamReader(is1)); try { String lineB = null; while ((lineB = br.readLine()) != null) { if (lineB != null) System.out.println(lineB); } } catch (IOException e) { e.printStackTrace(); } } }.start(); new Thread() { public void run() { BufferedReader br2 = new BufferedReader( new InputStreamReader(is2)); try { String lineC = null; while ((lineC = br2.readLine()) != null) { if (lineC != null) System.out.println(lineC); } } catch (IOException e) { e.printStackTrace(); } } }.start(); convertImgProcess.waitFor(); } catch (Exception e) { System.err.println(e); } } }
评论
1 楼
jiuyuehe
2012-03-01
博主你好,用了你方法以后果然没有阻塞了,但是原理还是不太明白,主要是由于线程的缓存没用清空?还是process的缓存没有情况?希望您能详细解答一下
发表评论
-
spring data redis源码框架分析
2012-02-06 21:49 7885redis是由Salvatore Sanfilippo用C语言 ... -
Java Enum
2011-10-08 20:31 1029public enum Operation { ... -
Java内部类访问
2011-10-08 17:37 900当内部类为非静态时: public class In ... -
JVM Method Table
2011-10-07 16:10 1232方法表 为了提高访问效率,必须仔细的设计存储在方法区中的 ... -
JVM Symbolic refer 2 Direct Refer
2011-10-06 17:31 2Method Area: Symbolic Referen ... -
【转载】JVM中方法区
2011-09-29 14:17 1086原文链接:http://hi.baidu. ... -
JVM初探
2011-09-29 13:49 845概念: JAVA的JVM的内存可分为5个区:堆(heap ... -
java ClassLoader
2011-09-28 17:02 545JVM规范定义了两种类型的类装载器:启动内装载器 (bo ... -
【转载】java 回调函数
2011-09-18 16:01 1041java“回调函数” 所谓 ... -
Java读取classpath中的文件
2011-08-29 11:06 7725/** * 类AppsImport.java的实现描 ... -
【转载】jvm的内存调优
2011-08-23 17:30 1365原文链接:http://k ... -
【转载】Java 垃圾回收策略调优,实践篇
2011-08-23 17:13 889转载自:http://www.douban.c ... -
[转载]线程sleep()和wait()的区别
2011-07-01 15:07 843线程sleep()和wait()的区别 ... -
checked Exception and RuntimeException
2011-05-29 15:25 9191. RuntimeException 虚拟机执行程序时,如果 ... -
【转载】编程式事务 声明式事务区别
2011-05-09 19:08 978原文链接:http://topic.csdn.net/u/20 ... -
[转载]java回调函数
2011-04-28 16:55 899转载自:http://www.blogjava.net/ ... -
【转载】jms topic与jms queue区别
2011-02-18 17:29 10913作者longdick http://longdick.i ... -
【转载】JAVA NIO 简介
2011-02-18 11:30 891原文链接:http://www.iteye.com/topic ... -
RTTI & Reflection(反射机制)
2011-01-16 15:45 2269RTTI & Reflection 一、RTTI(R ... -
Java编程思想中异常的限制
2011-01-08 20:55 10501. 构造函数必须抛出基类构造函数的抛出的异常; 这 ...
相关推荐
return pro.exitValue(); } /** * @param args */ public static void main(String[] args) { String sourcePath = "D:\\springFramework.pdf"; //源文件路径 String destPath = "D:\\"; //目标...
System.out.println("ffmpeg Process exitValue: " + exitVal); return true; } catch (Exception e) { System.out.println("ffmpeg exec cmd Exception " + e.toString()); } return false; }
For a process to access 3 GB of address space, the executable image must have been linked with the /LARGEADDRESSAWARE flag or modified using Imagecfg.exe. It should be pointed out that SQL Server was ...
Ctrl+d 跟exit一样效果,表中止本次操作。 logout 当csh时可用来退出,其他shell不可用。 clear 清屏,清除(之前的内容并未删除,只是没看到,拉回上面可以看回)。 五、目录管理命令 pwd 显示当前所在目录,打印...
An isolation level determines the degree to which data is isolated for use by one process and guarded against interference from other processes. Prior to SQL Server 7.0, REPEATABLE READ and ...
- Changed the Memory test to wait for the Video Playback test and 3D test to allow memory allocation for these tests. - Minor changes to the No operation error watchdog timer for the CD and Hard ...
-k language use keyboard layout (for example 'fr' for French) -audio-help print list of audio drivers and their options -soundhw c1,... enable audio support and only specified sound cards (comma ...
value "NO" does not have a valid configuration Interface IP-Address OK? Method Status Protocol Ethernet0 unassigned NO unset up up Serial0 unassigned NO unset up up ……… ……… … …… … … ...
- Added support for VGA graphics mode with 400 lines (partial fix for SF bug #2948724) - NE2K: Fixed "send buffer" command issue on big endian hosts - USB - converted common USB code plus devices ...
-gdb dev wait for gdb connection on 'dev' -s shorthand for -gdb tcp::1234 -d item1,... output log to /tmp/qemu.log (use -d ? for a list of log items) -hdachs c,h,s[,t] force hard disk 0 physical ...