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()用法及代码示例
- Java Process Destroy()用法及代码示例
- Java Process destroy()用法及代码示例
- Java Process isAlive()用法及代码示例
- Java Process getOutputStream()用法及代码示例
- Java Process exitValue()用法及代码示例
- Java Process getInputStream()用法及代码示例
- Java Process getErrorStream()用法及代码示例
- Java ProcessBuilder redirectErrorStream()用法及代码示例
- Java ProcessBuilder environment()用法及代码示例
- Java ProcessBuilder start()用法及代码示例
- Java ProcessBuilder directory()用法及代码示例
- Java Provider keySet()用法及代码示例
- Java Properties propertyNames()用法及代码示例
- Java Provider.Service getAttribute()用法及代码示例
- Java Provider getName()用法及代码示例
- Java Properties compute(Key, BiFunction)用法及代码示例
- Java Provider keys()用法及代码示例
- Java Properties containsKey(value)用法及代码示例
- Java Properties computeIfAbsent(Key, Function)用法及代码示例
注:本文由纯净天空筛选整理自 Java Process waitFor() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。