java - 如何告诉父进程等待子进程?

标签 java process

在我的java代码中,我有以下几行

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("another program");

我的理解是这会创建一个子进程。有没有办法使用父进程执行“另一个程序”?或者告诉父进程等待执行完成后再继续的方法?

最佳答案

Is there a way to execute "another program" using the parent process?

不,需要一个单独的过程。

...or a way to tell the parent to wait until the execution is finished before continuing?

Process#waitFor正是这样做的:

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

例如:

pr.waitFor();

关于java - 如何告诉父进程等待子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26103056/

相关文章:

java - Android复杂图形元素

java - 如何解决 Android Studio 编译时出现的与 .class 相关的文件未找到错误?

java - 如何在 Spring-Boot 微服务项目中实现 HATEOAS

java - ZXing 出错

java - Dozer:没有默认构造函数的类映射

java - 如何通过不同的java程序来运行一个java程序?

windows - 批处理 - 如果正在运行则终止程序,如果没有则启动它

linux - C 中进程的不同实例分配不同的任务

c - 如何在C中按顺序执行子进程

linux - 线程和进程的内存分配