ProcessBuild.command(commend);
 Process process= ProcessBuild.start(); //
 public void run() {
        try {
            System.out.println( "waitFor()");
            process.waitFor();
        }
        catch( InterruptedException e) {
            throw new RuntimeException( "InterruptedException Exception encountered", e);
        }
        if( pro.exitValue() != 0) {  
            process.destroy();
        }
        System.out.println( "exitValue");
    }不知道process.waitFor();这个方法会让当前线程一直等待下去不会停止,我想知道他结束之后能够打印出退出的消息.我就是想实现子进程结束后通知当前线程
希望各位能帮帮忙~~

解决方案 »

  1.   

    process.waitFor();
    是个组塞方法,如果子进程没有结束,它是不会返回的,等子进程结束了,程序自然会继续执行下去,你要问什么?
      

  2.   

    我开的子进程是个转换视频格试的可执行程序"ffmpeg.exe"
    它转换一个文件完成大概要30秒,不加process.waitFor();的话它30秒后就完成了我要先判断它的退出值是不是0如果是0的话就表示正常终止
    但直接distory()的话可能还没有完成就终止了我现实现的是子进程完成了通知一下
      

  3.   

    jay584930074() ( ) 信誉:100    Blog  2007-02-28 11:17:00  得分: 0  
     
     
       我开的子进程是个转换视频格试的可执行程序"ffmpeg.exe"
    它转换一个文件完成大概要30秒,不加process.waitFor();的话它30秒后就完成了我要先判断它的退出值是不是0如果是0的话就表示正常终止
    但直接distory()的话可能还没有完成就终止了我现实现的是子进程完成了通知一下
      
     
    --------------------------
    你的意思是你要是加了process.waitFor();
    即时你的子进程结束了,程序也不执行下去???真是活见鬼了。
      

  4.   

    但如果我将WEB服务器停止时它就执行完成了
      

  5.   

    按照API,process.waitFor();方法会阻塞,一直到子进程结束为止,然后继续运行下去,没有所谓的通知不通知的,你把子进程结束后想要做的工作,写在这个方法后面就可以了。你可以测试一下,
    public class ProcessTest {
    public static void main(String args[]){
    ProcessBuilder pb = new ProcessBuilder();
    pb.command(new String[]{"notepad.exe"});
    try {
    Process p = pb.start();
    p.waitFor();
    System.out.println(p.exitValue());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }程序打开notepad后,将阻塞在waitFor()方法上,关闭notepad后,程序会继续执行,打印出结果
      

  6.   

    楼主,我也有你同样的问题呀,痛苦中的。
    try {
    ProcessBuilder builder = new ProcessBuilder();
    builder.command(commend);
    Process subprocess = builder.start();
    subprocess.waitFor();
    System.out.println(subprocess.exitValue());
    return true;
    } catch (Exception e) {
    e.printStackTrace();
    return false;
    }现在我的这段代码执行的情况是,子进程(subprocess)一直在等待主进程结束后,它执行,怎么刚好相反呢
      

  7.   

    这个问题前不久我刚遇到过,死锁了,看下帮助文档就知道了。
    javadoc
    The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. 
    最后一段话,死锁的原因是子线程的输出流或输入流缓存太小,所以必须自己手动清空缓存。
    在process.waitFor();之前加上一下代码
    BufferedReader br = new BufferedReader(process.getInputStream());
    while(br.readLine()!=null);通常这样可以解决,但是线程执行是由cpu控制的,如果process还没被执行,那么while(br.readLine()!=null);就会结束,此时如果process刚好被执行了,但由于while(br.readLine()!=null);已经结束了,process的输入流输出流还是没有被清空,到process.waitFor();时还是会造成堵塞的。所以,一般我的做法是把上面的代码写到一个监视线程中,比如
    class WatchThread extends Thread {
        Process p;
        boolean over;
        public WatchThread(Process p) {
            this.p = p;
            over = false;
        }    public void run() {
            try {
                if (p == null) return;
                BufferedReader br = new BufferedReader(p.getInputStream());
                while (true) {
                    if (p==null || over) {
                        break;
                    }
                    while(br.readLine()!=null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public void setOver(boolean over) {
            this.over = over;
        }
    }然后在process.waitFor()之前,添加
    WatchThread wt = new WatchThread(process);
    wt.start();
    然后在process.waitFor()之后,添加
    wt.setOver(true);仅供参考