目录

abstract int waitFor()

描述 (Description)

如果需要, java.lang.Process.waitFor()方法会导致当前线程等待,直到此Process对象表示的进程终止。 如果子进程已终止,则此方法立即返回。 如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出。

声明 (Declaration)

以下是java.lang.Process.waitFor()方法的声明

public abstract int waitFor()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回进程的退出值。 按照惯例,0表示正常终止。

异常 (Exception)

NA

例子 (Example)

以下示例显示了lang.Process.waitFor()方法的用法。

package com.iowiki;
public class ProcessDemo {
   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         Process p = Runtime.getRuntime().exec("notepad.exe");
         // cause this process to stop until process p is terminated
         p.waitFor();
         // when you manually close notepad.exe program will continue here
         System.out.println("Waiting over.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Creating Process...
Waiting over.
↑回到顶部↑
WIKI教程 @2018