gpt4 book ai didi

Java ProcessBuilder 启动多个java进程

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:19 27 4
gpt4 key购买 nike

在给定相同的 Main.class、参数和 VM 选项的情况下,我可以生成多个不同的 JVM 吗?是否可以使用 ProcessBuilder 来实现?

最佳答案

这是一个使用 Process 启动 10 个不同 JVM 进程的基本示例:

   for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {

public void run() {
try {
//start a new jvm with 256m of memory with the MyClass passing 2 parameters
String cmd = "java -Xmx256M -cp myjar.jar com.mymainclass.MyClass par1 par2";
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
while (line != null) {
line = br.readLine();
}
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
line = br.readLine();
while (line != null) {
line = br.readLine();
}
} catch (IOException e) {
}

}
}).start();
}

关于Java ProcessBuilder 启动多个java进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com