开发者

How to pause process run using Java's ProcessBuilder.start()?

Alright, so I'm writing this program that essentially batch runs other java programs for me (multiple times, varying parameters, parallel executions, etc).

So far the running part works great. Using ProcessBuilder's .start() method (equivalent to the Runtime.exec() I believe), it creates a separate java process and off it goes.

Problem is I would like to be able to pause/stop these processes once they've been started. With simple threads this is generally easy to do, however the external process doesn't seem to have any inbuilt functionality for waiting/sleeping, at least not from an external point of view.

My question(s) is this: Is there a way to pause a java.lang.Process object? If not, does anyone know of any related exec libraries that do contain this ability?开发者_如何学编程 Barring all of that, is extending Process a more viable alternative?


My question(s) is this: Is there a way to pause a java.lang.Process object?

As you've probably discovered, there's no support for this in the standard API. Process for instance provides no suspend() / resume() methods.

If not, does anyone know of any related exec libraries that do contain this ability?

On POSIX compliant operating systems such as GNU/Linux or Mac OS you could use another system call (using Runtime.exec, ProcessBuilder or some natively implemented library) to issue a kill command.

Using the kill command you can send signals such as SIGSTOP (to suspend a process) and SIGCONT (to resume it).

(You will need to get hold of the process id of the external program. There are plenty of questions and answers around that answers this.)


You will need to create a system for sending messages between processes. You might do this by:

  1. Sending signals, depending on OS. (As aioobe notes.)
  2. Having one process occasionally check for presence/absence of a file that another process can create/delete. (If the file is being read/written, you will need to use file locking.)
  3. Have your "main" process listen on a port, and when it launches the children it tells them (via a comamnd-line argument) how to "phone home" as they start up. Both programs alternate between doing work and checking for handling messages.

From what you have described (all Java programs in a complex batch environment) I would suggest #3, TCP/IP communication.

While it certainly involves extra work, it also gives you the flexibility to send commands or information of whatever kind you want between different processes.


A Process represents a separate process running on the machine. Java definitely does not allow you to pause them through java.lang.Process. You can forcibly stop them using Process.destroy(). For pausing, you will need the co-operation of the spawned process.

What sorts of processes are these? Did you write them?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜