Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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_Concurrency_Callable - Fatal编程技术网

Java 可调用项的执行顺序不一致

Java 可调用项的执行顺序不一致,java,multithreading,concurrency,callable,Java,Multithreading,Concurrency,Callable,我有这样的代码,我在其中执行可调用的集合,我需要一个集合来完成所有的工作,然后再触发下一个集合。这段代码似乎工作正常,但有时下一组代码会在时间到来之前开始运行。这里怎么了 private void executeSubGraph(QuestExecutionContext ctx, Set<Activity> subGraph, int progressAfterRan) { ExecutorService pool = Executors.newFixedThreadPoo

我有这样的代码,我在其中执行可调用的集合,我需要一个集合来完成所有的工作,然后再触发下一个集合。这段代码似乎工作正常,但有时下一组代码会在时间到来之前开始运行。这里怎么了

private void executeSubGraph(QuestExecutionContext ctx, Set<Activity> subGraph, int progressAfterRan) {
    ExecutorService pool = Executors.newFixedThreadPool(16);
    subGraph.forEach(a -> {
        ActivityRunner<? extends Activity> runner = activityRunnerFactory.getRunner(ctx, a);
        if (runner != null) {
            Callable<List<PortValuePart>> runnerCallable = () -> {
                try {
                    LOG.info("Running {} in {}", a, a.getClass() );
                    List<PortValuePart> result = runner.call();
                    LOG.info("Result of {} in {} is {}", a, a.getClass(), result);
                    if (result != null) {
                        result.forEach(r -> resultProcessor.processResult(new PortValuePartEnvelope(r)));
                    }
                    return result;
                } catch (Exception e) {
                    LOG.warn("Exception for {} in {}", a, runner.getClass(), e);
                    resultProcessor.processResult(Progress.failed(ctx.getId(), e));
                    throw new RuntimeException(e);
                }
            };
            Future<List<PortValuePart>> p = pool.submit(runnerCallable);
        } else {
            LOG.warn("No runner found for activity {}", a);
            resultProcessor.processResult(Progress.failed(ctx.getId(), new RuntimeException("No runner found for activity " + a)));
            throw new RuntimeException("No runner found for activity " + a);
        }
    });

    pool.shutdown();

    try {
        pool.awaitTermination(WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
        resultProcessor.processResult(Progress.running(ctx.getId(), progressAfterRan));
    } catch (InterruptedException e) {
        throw new PlatformException("Execution interrupted.");
    }
}
private void executeSubGraph(QuestExecutionContext ctx,Set子图,int progressAfterRan){
ExecutorService池=Executors.newFixedThreadPool(16);
子图.forEach(a->{
ActivityRunner请注意,如果超时,它不会抛出异常;它只返回
false
。如果要确保下一个调用不会与这些调用同时运行,您可能应该使用返回值,如果时间太长,可能会抛出异常(并终止任务)