當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。