Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 启动可执行文件的多个实例并对其进行管理_Java_Multithreading_Exec_Pid - Fatal编程技术网

Java 启动可执行文件的多个实例并对其进行管理

Java 启动可执行文件的多个实例并对其进行管理,java,multithreading,exec,pid,Java,Multithreading,Exec,Pid,我有一个进程运行的许多障碍 Runtime rt = Runtime.getRuntime(); int i=0; int arg1; while(i<10){ arg1 = i+1; Process p = rt.exec("abc.exe "+ arg1); i++; } Runtime rt=Runtime.getRuntime(); int i=0; int arg1; while(iRuntime.exec(…)命令返回一个Process对象。您可以将P

我有一个进程运行的许多障碍

Runtime rt = Runtime.getRuntime();
int i=0;
int arg1;
while(i<10){
    arg1 = i+1;
    Process p = rt.exec("abc.exe "+ arg1);
    i++;
}
Runtime rt=Runtime.getRuntime();
int i=0;
int arg1;
while(iRuntime.exec(…)
命令返回一个
Process
对象。您可以将
Process
对象放入一个集合,然后使用
Process.exitValue()
方法查看每个进程是否已完成。
exitValue()
如果进程仍在运行,则抛出
非法线程状态异常

因此,您的代码可能类似于:

List<Process> processes = new ArrayList<Process>();
// noticed I turned your while loop into a for loop
for (i = 0; i < 10 i++) {
    int arg1 = i + 1;
    Process p = rt.exec("abc.exe "+ arg1);
    processes.add(p);
}
...
// watch them to see if any of them has finished
// this can be done periodically in a thread
for (Process process : processes) {
   try {
       if (process.exitValue() != 0) {
           // it did not exit with a 0 so restart it
           ...
       }
   } catch (IllegalThreadStateException e) {
       // still running so we can ignore the exception
   }
}
List进程=new ArrayList();
//注意到我把你的while循环变成了for循环
对于(i=0;i<10 i++){
int arg1=i+1;
进程p=rt.exec(“abc.exe”+arg1);
增加(p);
}
...
//观察他们,看看是否有人完成了
//这可以在线程中定期完成
用于(流程:流程){
试一试{
if(process.exitValue()!=0){
//它没有以0退出,因此请重新启动它
...
}
}捕获(非法){
//仍在运行,因此我们可以忽略异常
}
}
我可以在Linux和windows上跟踪这件事吗


如果我理解这个问题,上面的代码应该可以在Lunux和Windows上运行。

使用processbuilder,然后保留进程id,您可以使用该id管理启动的任何进程。
Runtime.exec(…)
应该保留用于需要执行的“一次性”命令