如何在 Java 中使用多处理 - 而不是多线程
我一直在寻找一个用于简单的基于 java 的多处理的 api,但找不到任何东西。
我有遗留代码,需要将其集成到我的 java 应用程序中。由于这个遗留(本机)代码有时会崩溃,整个 jvm 也会随之崩溃。所以我想做的是在不同的进程(而不是线程!)中运行此代码及其适配器。
较新的 java jdks 中有 ProcessBuilder,它可以让您启动一个新进程并为您提供一个输入/输出流;所以可以手动解决我的问题。为此,您必须找到 jvm 的路径并使用您的进程启动它。那么就必须使用流来进行通信。
有什么东西可以接管这项工作吗?或者我真的必须手工完成吗?
I've been looking for an api for simple java-based multiprocessing and couldn't find anything.
I have legacy code which I need to integrate into my java application. Since this legacy (native) code crashes sometimes, the whole jvm crashes with it. So what I want to do, is to run this code and its adapter in a different process (not thread!).
There's the ProcessBuilder in the newer java jdks, it lets you start a new process and gives you a In/Outputstream; so solving my problem is possible by hand. In order to do so, you have to find the path to your jvm and start it up with the for your process. Then you have to use the stream to communicate.
Is there something which takes over that job? Or do I really have to do it by hand?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AFAIK,人们通常用手来做这件事。
问题是没有真正可移植的方法......很难(不可能)识别当前运行的 JVM 是从哪里启动的,而且 JVM 命令行选项是供应商、版本和(可能)特定于平台。
简单的解决方案是将 JVM 路径和启动子 JVM 的选项放入某个配置文件中。
AFAIK, people usually do this by hand.
The problem is that there's no truly portable way of doing it ... what with the difficulty (impossibility) of identifying where the currently running JVM was launched from, and the fact that JVM command-line options are vendor, version and (potentially) platform specific.
The simple solution is just to put the JVM path and options for launching the child JVM into some configuration file.
您可以使用 -Dprocess.name=$1 并让您的主类接受一些命令行参数。您可以通过调用以下内容来调用:
java -cp $CLASSPATH $VM_ARGS $MAIN_CLASS
并且您的 VM_ARGS 可以定义为
VM_ARGS=" -Dprocess.name=$1"
You can use the -Dprocess.name=$1 and let your main class take in some command line args. You can invoke by calling something like this:
java -cp $CLASSPATH $VM_ARGS $MAIN_CLASS
and your VM_ARGS can be defined something as
VM_ARGS=" -Dprocess.name=$1"
FWIW,我编写了一个替换类来处理大量 I/O 流重定向问题,位于 david.tribble.com/src/java/tribble/util/RuntimeExec.java
FWIW, I wrote a replacement class to take care of a lot of the I/O stream redirection nastiness, at david.tribble.com/src/java/tribble/util/RuntimeExec.java
您正在寻找称为 Java RMI(Remolt 方法调用)的技术。
这允许一个 JVM 调用另一个 JVM 中的方法。这可以在同一台计算机上或通过网络。
You are looking for the technology called Java RMI (Remolt Method Invocation).
This allows one JVM to call a method in another JVM. This can be on the same machine or over a network.