如何使用 Java 的 ProcessBuilder.start() 暂停进程运行?
好吧,所以我正在编写这个程序,它本质上是为我批量运行其他 java 程序(多次、改变参数、并行执行等)。
到目前为止,跑步部分效果很好。使用 ProcessBuilder 的 .start() 方法(我相信相当于 Runtime.exec()),它创建一个单独的 java 进程并开始运行。
问题是我希望能够在这些进程启动后暂停/停止它们。对于简单的线程,这通常很容易做到,但是外部进程似乎没有任何内置的等待/睡眠功能,至少从外部的角度来看是这样。
我的问题是:有没有办法暂停 java.lang.Process 对象?如果没有,有人知道任何包含此功能的相关执行库吗?除此以外,扩展 Process 是否是一个更可行的替代方案?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您可能已经发现的,在标准API。例如,
Process
不提供suspend()
/resume()
方法。在 POSIX 兼容操作系统(例如 GNU/Linux 或 Mac OS)上,您可以使用另一个系统调用(使用 Runtime.exec、ProcessBuilder 或某些本机实现的库)发出
kill
命令。使用
kill
命令,您可以发送诸如SIGSTOP
(暂停进程)和SIGCONT
(恢复进程)等信号。(您需要获取外部程序的进程ID。有很多问题 以及围绕这个问题的答案。)
As you've probably discovered, there's no support for this in the standard API.
Process
for instance provides nosuspend()
/resume()
methods.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 akill
command.Using the
kill
command you can send signals such asSIGSTOP
(to suspend a process) andSIGCONT
(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.)
您将需要创建一个用于在进程之间发送消息的系统。您可以通过以下方式执行此操作:
根据您的描述(复杂批处理环境中的所有 Java 程序),我建议#3,TCP/IP 通信。
虽然它确实涉及额外的工作,但它也使您可以灵活地在不同进程之间发送您想要的任何类型的命令或信息。
You will need to create a system for sending messages between processes. You might do this by:
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.
进程代表机器上运行的单独进程。 Java 绝对不允许你通过 java.lang.Process 来暂停它们。您可以使用 Process.destroy()。对于暂停,您将需要生成的进程的合作。
这些是什么类型的过程?它们是你写的吗?
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?