`
leonmau
  • 浏览: 103094 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[转载]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的缓存没有情况?希望您能详细解答一下

相关推荐

    pdf2swf将PDF转换成SWF

    return pro.exitValue(); } /** * @param args */ public static void main(String[] args) { String sourcePath = "D:\\springFramework.pdf"; //源文件路径 String destPath = "D:\\"; //目标...

    FFmpeg(liunx中amr转MP3工具)

    System.out.println("ffmpeg Process exitValue: " + exitVal); return true; } catch (Exception e) { System.out.println("ffmpeg exec cmd Exception " + e.toString()); } return false; }

    微软内部资料-SQL性能优化2

    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 ...

    2009 达内Unix学习笔记

    Ctrl+d 跟exit一样效果,表中止本次操作。 logout 当csh时可用来退出,其他shell不可用。 clear 清屏,清除(之前的内容并未删除,只是没看到,拉回上面可以看回)。 五、目录管理命令 pwd 显示当前所在目录,打印...

    微软内部资料-SQL性能优化3

    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 ...

    BURNINTEST--硬件检测工具

    - 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 ...

    Qemu-1.0.1 for windows

    -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 ...

    CISCO 技术大集合

    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 ……… ……… … …… … … ...

    Bochs - The cross platform IA-32 (x86) emulator

    - 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 ...

    qemu-0.13.0(编译过全处理器支持)

    -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 ...

Global site tag (gtag.js) - Google Analytics