Java中进程的定时并行超时

时间:2015-09-20 10:45:53

标签: java process parallel-processing timeout

我想推出一系列流程同时工作 - 所以我创建了一个流程数组,并像这样推出它们:

    Process[] p_ids= new Process[ids.length];
    int index = 0;
    //launching the processes
    for (int id: ids) {
        String runProcessCommand = createCommandForProcess();
        System.out.println(runProcessCommand);
        try {
            p_ids[index] = Runtime.getRuntime().exec(runProcessCommand);
            index++;
        } catch (IOException e) {
        }
    }

之后我想等待所有人完成。所以我采用相同的进程数组并迭代其中的所有进程,每次迭代我等待当前迭代进程完成或等待特定时间通过。

像这样:

    for (Process p_id: p_ids) {
        try {
            //timeout for waiting a process
            p_id.waitFor(timeoutForSingleProcess, TimeUnit.HOURS);
            //error handling when reaching timeout
        } catch (InterruptedException e) {
            System.err.println("One of the process's execution time exceeded the timeout limit");
            e.printStackTrace();
        }
    }

问题在于我想给出total_time_out - 意味着每个进程都有一个固定的超时时间。

说我有process1,process2,process3。我想暂停1小时。如果每个进程(1,2或3)都需要一个多小时才能完成,我希望超时能够启动。

我的代码中的问题是超时开始倒计时 - 当它转到循环时(而不是与其他进程同时)。即如果process1需要0.5小时而进程2需要1小时 - 两个进程将同时启动进程2超时将在午餐后0.5小时开始倒计时(因为我们等了0.5小时对于流程1,然后转移到流程2)。那样应该激活的超时 - 被忽略了。

是否有任何流程池或类似的东西可以帮助我?

0 个答案:

没有答案