当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Process waitFor()用法及代码示例


Process类的waitFor()方法用于等待当前正在执行的线程,直到Process对象执行的进程完成。该方法在子进程终止后立即返回,如果子进程没有终止,线程将被阻塞。

用法

public abstract int waitFor()

参数

NA

返回

该方法一般返回进程和子进程的退出值。默认情况下,正常终止由 0 表示。

例子1

public class Process_waitForMethodExample1 {
    public static void main(String[] args) {
        try {
            // creating a new process.
            System.out.println("Ms Paint will run.");
            Process p = Runtime.getRuntime().exec("mspaint.exe");
            // waits for the process until you terminate.
            p.waitFor();
            // when you manually close Ms Paint.exe program will continue here
            System.out.println("You have exited from Ms paint.");
        } catch (Exception ex) {
            System.out.println("exception is:"+ex);
        }
    }
}

输出:

Ms Paint will run.
You have exited from Ms paint.

例子2

public class Process_waitForMethodExample2 {
    public static void main(String[] args) {
        try {
            // creating a new process.
            System.out.println("Ms Paint will run.");
            Process p1 = Runtime.getRuntime().exec("mspaint.exe");
            // waits for the process until you terminate.
            p1.waitFor();
            // when you manually close notepad.exe program will continue here
            System.out.println("You have exit from Ms-paint.");
            System.out.println("Notepad will run.");
            Process p2 = Runtime.getRuntime().exec("notepad.exe");
            // waits for the process until you terminate.
            p2.waitFor();
            // when you manually close notepad.exe program will continue here
            System.out.println("You have exited from Notepad.");

        } catch (Exception ex) {
            System.out.println("Oops! an error has occurred.");             }
    }
}

输出:

Ms Paint will run.
You have exited from Ms-paint.
Notepad will run.
You have exited from Notepad.

例子3

public class Process_waitForMethodExample3 {
    public static void main(String[] args) {

try{
    System.out.println("Both Ms paint and Notepad will run simultaneously.\n");
        System.out.println("1. Ms Paint will run.");
        Process p1 = Runtime.getRuntime().exec("mspaint.exe");
    System.out.println("2. Notepad will run.\n\n");
    Process p2 = Runtime.getRuntime().exec("notepad.exe");
        // waits for the process until you terminate.
        p1.waitFor();
        // when you manually close notepad.exe program will continue here
        System.out.println("You have exit from Ms-paint.");
    System.out.println("You have exited from Notepad.");
        p2.waitFor();
    } catch (Exception ex) {
        System.out.println("Oops! an error has occurred."); 
    }
   }
 }

输出:

Both Ms paint and Notepad will run simultaneously.

1. Ms Paint will run.
2. Notepad will run.

You have exited from Ms-paint.
You have exited from Notepad.



相关用法


注:本文由纯净天空筛选整理自 Java Process waitFor() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。