开发者

How to use multiprocessing with Java - NOT multithreading

开发者 https://www.devze.com 2023-01-19 09:28 出处:网络
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

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 th开发者_JAVA百科read!).

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?


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.


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


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.

0

精彩评论

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