我对Runtime.getRuntime()
有疑问。
我正在运行以下代码。
public class TestMain {
public static void main(String[] args) {
Runtime rs = Runtime.getRuntime();
rs.exec("C:\\Windows\\System32\\sampleProgram.exe");
rs.exec("C:\\Windows\\System32\\sampleProgram2.exe");
rs.exec("C:\\Windows\\System32\\sampleProgram3.exe");
}
}
现在,如果sampleProgram
需要更多时间来执行,控件是否会等待sampleProgram
完成,或者它将转到sampleProgram2
并将并行运行?请帮忙。
答案 0 :(得分:5)
不,它不等。
但您可以等待它使用waitFor
方法完成。
rs.exec("C:\\Windows\\System32\\sampleProgram3.exe").waitFor();
注意:如果您使用cmd start xxxxxx
之类的命令,waitFor
方法确实无法正常工作,因为start
命令会启动给定的命令/另一个线程中的参数(这当然是一个特定于Windows的东西)。