pnv
pnv

Reputation: 1499

Processes in Java- Do they work concurrently?

I am executing another java program through a Process object.
This takes some time to complete. In the mean time, will the parent program continue running or will it be on hold, to wait for the child process to stop?

I know that two threads can run in parallel, can a thread be used to execute this other program?

Please let me know if you need any more details, if this became too abstract.

Upvotes: 0

Views: 140

Answers (2)

Joni
Joni

Reputation: 111239

The two processes are independent and run in parallel. The parent can wait for the child to terminate calling the waitFor method in the Process class.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500375

It will execute separately unless you either block waiting for data from the other process (e.g. calling Process.getInputStream() and then reading from it), or call Process.waitFor(). The second process has its own threads - these are not the threads of the process that happens to start the second process.

Of course, it's possible that both processes will end up dealing with the same resource and have to cooperate in that sense - but in general, using multiple processes is a separation level up from threads. (It's relatively tricky to get processes to access the same memory, to get their threads to coordinate with each other, etc.)

Upvotes: 7

Related Questions