DJames
DJames

Reputation: 689

How do I tell a parent process to wait for a child process?

In my java code, I have the following lines

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

My understanding is this creates a child process. 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?

Upvotes: 0

Views: 448

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074038

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

No, a separate process is required.

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

Process#waitFor does exactly that:

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.

E.g.:

pr.waitFor();

Upvotes: 1

Related Questions