流程类的waitFor()方法 (Process Class waitFor() method)
waitFor() method is available in java.lang package.
在java.lang包中提供了waitFor()方法 。
waitFor() method is used to causes the currently running thread to wait when required until the process denoted by this Process object has completed its termination.
waitFor()方法用于使当前正在运行的线程在需要时等待,直到由该Process对象表示的进程完成其终止为止。
waitFor() method returns when the process has already terminated and when the process has not already terminated the invoking thread will be blocked until the process terminates.
当进程已经终止并且进程尚未终止时, waitFor()方法将返回,直到进程终止,调用线程才会被阻塞。
waitFor() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
waitFor()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
waitFor() method does not throw an exception at the time of process termination.
在进程终止时, waitFor()方法不会引发异常。
Syntax:
句法:
public abstract int waitFor();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is int, it returns the exit value of the process and when it returns value 0 so it reflects normal termination of the process.
此方法的返回类型为int ,它返回流程的退出值,当返回值0时,它反映了流程的正常终止。
Example:
例:
// Java program to demonstrate the example
// of int waitFor() method of Process
import java.io.*;
import java.util.*;
public class WaitFor {
public static void main(String[] args) throws Exception {
// Instantiating Process object
System.out.println("Process Instantiated");
Process pr = Runtime.getRuntime().exec("notepad.exe");
// By using waitFor() method is to stop
// current process until other process
// finish its execution
pr.waitFor();
// when we close current going process manually and so //waiting
// process can continue its execution
System.out.println("Waiting process can resume");
}
}
Output
输出量
Process Instantiated
Waiting process can resume
翻译自: https://www.includehelp.com/java/process-waitfor-method-with-example.aspx