如何使用 Java 的 ProcessBuilder.start() 暂停进程运行?

发布于 2024-11-13 08:03:12 字数 345 浏览 3 评论 0原文

好吧,所以我正在编写这个程序,它本质上是为我批量运行其他 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凹づ凸ル 2024-11-20 08:03:12

我的问题是:有没有办法暂停 java.lang.Process 对象?

正如您可能已经发现的,在标准API。例如,Process 不提供 suspend() / resume() 方法。

如果没有,是否有人知道任何包含此功能的相关执行库?

在 POSIX 兼容操作系统(例如 GNU/Linux 或 Mac OS)上,您可以使用另一个系统调用(使用 Runtime.exec、ProcessBuilder 或某些本机实现的库)发出 kill 命令。

使用 kill 命令,您可以发送诸如 SIGSTOP (暂停进程)和 SIGCONT (恢复进程)等信号。

(您需要获取外部程序的进程ID。有很多问题 以及围绕这个问题的答案。)

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.)

趁年轻赶紧闹 2024-11-20 08:03:12

您将需要创建一个用于在进程之间发送消息的系统。您可以通过以下方式执行此操作:

  1. 发送信号,具体取决于操作系统。 (正如 aioobe 指出的那样。)
  2. 让一个进程偶尔检查另一进程可以创建/删除的文件是否存在。 (如果正在读取/写入文件,您将需要使用文件锁定。)
  3. 让您的“主”进程侦听端口,当它启动子进程时,它告诉它们(通过命令行参数)如何“当他们启动时打电话回家”。这两个程序交替执行工作和检查处理消息。

根据您的描述(复杂批处理环境中的所有 Java 程序),我建议#3,TCP/IP 通信。

虽然它确实涉及额外的工作,但它也使您可以灵活地在不同进程之间发送您想要的任何类型的命令或信息。

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.

神经暖 2024-11-20 08:03:12

进程代表机器上运行的单独进程。 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文