简体   繁体   English

Java 7:如何批量执行并行任务?

[英]Java 7: How to execute parallel tasks in batches?

I have three web-service calls that can run in parallel. 我有三个可以并行运行的Web服务调用。 Hence, I'm using a fixed pool of 3 threads to run them. 因此,我使用3个线程的固定池来运行它们。

Now I want to process a couple more web-service calls, that can run in parallel, but only after the first three calls are processed. 现在我想处理几个可以并行运行的Web服务调用,但只能在处理前三个调用之后。

How can I batch them? 我怎么批处理它们? I want the ones inside a batch to run in parallel. 我希望批处理中的那些并行运行。 And every batch only runs after the previous batch is completed. 并且每个批次仅在上一批次完成后运行。

So far I am only working with three services. 到目前为止,我只使用三项服务。 How can I batch them and start using another 2 services? 如何批量处理并开始使用其他2项服务?

  ExecutorService peopleDataTaskExecutor = Executors.newFixedThreadPool(3);

  Future<Collection<PeopleInterface>> task1 = null;
  if (condition) { 
      task1 = peopleDataTaskExecutor.submit(buildTask1Callable(mycontext));
  }

  Future<Map<String, Task2Response>> task2 = peopleDataTaskExecutor.submit(buildTask2Callable(mycontext));

  Future<Map<String, Task3Response>> task3 = null;
  task3 = peopleDataTaskExecutor.submit(buildTask3Callable(mycontext));

  peopleDataTaskExecutor.shutdown();
  try {
      peopleDataTaskExecutor.awaitTermination(10, TimeUnit.SECONDS); 
  } catch (InterruptedException e) {
  }

  Collection<PeopleInterface> task1Data = null;
  try {
      task1Data = task1 != null ? task1.get() : null;
  } catch (InterruptedException | ExecutionException e) {
  }

  Map<String, Task2Response> task2Data = null;
  try {
      task2Data = task2.get();
  } catch (InterruptedException | ExecutionException e) {
  }

  Map<String, Task3Response> task3Data = null;
  if (task3 != null) {
      try {
          task3Data = task3.get();
      } catch (InterruptedException | ExecutionException e) {
      }
  }

The easiest way to execute batches sequentially is to use the invokeAll() method. 按顺序执行批处理的最简单方法是使用invokeAll()方法。 It accepts a collection of tasks, submits them to the executor and waits until completion (or until a timeout expires). 它接受一组任务,将它们提交给执行程序并等待直到完成(或直到超时到期)。 Here's a simple example that executes three batches sequentially. 这是一个按顺序执行三个批处理的简单示例。 Each batch contains three tasks running in parallel: 每批包含三个并行运行的任务:

public class Program {
    static class Task implements Callable<Integer> {
        private static Random rand = new Random();
        private final int no;
        Task(int no) {
            this.no = no;
        }
        @Override
        public Integer call() throws Exception {
            Thread.sleep(rand.nextInt(5000));
            System.out.println("Task " + no + " finished");
            return no;
        }
    }

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        processBatch(executor, 1);
        processBatch(executor, 2);
        processBatch(executor, 3);
        executor.shutdown();
    }

    private static void processBatch(ExecutorService executor, int batchNo) throws InterruptedException {
        Collection batch = new ArrayList<>();
        batch.add(new Task(batchNo * 10 + 1));
        batch.add(new Task(batchNo * 10 + 2));
        batch.add(new Task(batchNo * 10 + 3));
        List<Future> futures = executor.invokeAll(batch);
        System.out.println("Batch " + batchNo + " proceseed");
    }
}

You can use those Future s in the processBatch() method to check the completion states of the tasks (were they executes successfully or terminated because of an exception), obtain their return values etc. 您可以在processBatch()方法中使用那些Future来检查任务的完成状态(它们是否成功执行或因异常而终止),获取它们的返回值等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM