Beispiel #1
0
  /**
   * Return strings including the integers -1 through -10.
   *
   * @param workerPool
   */
  private static void listExample(WorkerPool workerPool) {
    List<Callable<String>> taskList = new ArrayList<Callable<String>>();
    for (int i = 1; i <= 10; i++) {
      final String input = Integer.toString(-1 * i);
      Callable<String> callable =
          new Callable<String>() {
            @Override
            public String call() throws Exception {
              // do all your parallelizable work here
              return "R(" + input + ")";
            }
          };
      taskList.add(callable);
    }

    List<String> resultList = workerPool.invokeAll(taskList);

    for (String result : resultList) {
      System.out.println("result: " + result);
    }
  }
Beispiel #2
0
  /**
   * Calculate the squares of the integers 1 through 10.
   *
   * @param workerPool
   */
  private static void mapExample(WorkerPool workerPool) {
    Map<Integer, Callable<Integer>> taskMap = new HashMap<Integer, Callable<Integer>>();
    for (int i = 1; i <= 10; i++) {
      final int input = i;
      Callable<Integer> callable =
          new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
              // do all your parallelizable work here
              return input * input;
            }
          };
      taskMap.put(i, callable);
    }

    Map<Integer, Integer> resultMap = workerPool.invokeAll(taskMap);

    for (Map.Entry<Integer, Integer> entry : resultMap.entrySet()) {
      System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
    }
  }