completablefuture任务不能并行运行

pxy2qtax  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(503)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

上个月关门了。
改进这个问题
我在哪里有问题 CompletableFuture 循环中的任务不是并行运行,而是同步运行:

  1. List<List<File>> filesLists = divideArrayIntoChunks(Arrays.asList(filesArray), 10);
  2. // number of tasks
  3. int numberOfTasks = filesLists.size();
  4. List<CompletableFuture<TreeMap<String, HashMap<String, Integer>>>> builderPartsMapFutures = Lists
  5. .newArrayList();
  6. for (int i = 0; i < numberOfTasks; i++) {
  7. List<File> filesList = filesLists.get(i);
  8. builderPartsMapFutures.add(getCompetableFutureResult(filesList, fileNameAndCharactersAmountMatchersMap,
  9. namesElementsMap, namesAndFileNamesMatchingCharactersMap));
  10. }
  11. @SuppressWarnings("rawtypes")
  12. CompletableFuture[] futureResultArray = builderPartsMapFutures
  13. .toArray(new CompletableFuture[builderPartsMapFutures.size()]);
  14. CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(futureResultArray);
  15. combinedFuture.get();
  16. CompletableFuture<List<TreeMap<String, HashMap<String, Integer>>>> finalResults = combinedFuture.thenApply(
  17. voidd -> builderPartsMapFutures.stream().map(future -> future.join()).collect(Collectors.toList()));
  18. finalResults.thenAccept(result -> System.out.println(result));

这是 getCompetableFutureResult() 方法:

  1. public static CompletableFuture<TreeMap<String, HashMap<String, Integer>>> getCompetableFutureResult(
  2. List<File> filesList, HashMap<String, Integer> fileNameAndCharactersAmountMatchersMap,
  3. TreeMap<String, ArrayList<String>> namesElementsMap,
  4. TreeMap<String, Map<String, Integer>> namesAndFileNamesMatchingCharactersMap) {
  5. return CompletableFuture
  6. .supplyAsync(() -> buildNamesAndFileNamesWithMatchingCharactersMapForEachChunk(filesList,
  7. fileNameAndCharactersAmountMatchersMap, namesElementsMap,
  8. namesAndFileNamesMatchingCharactersMap));
  9. }
b1uwtaje

b1uwtaje1#

  1. public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)

返回一个新的completablefuture,该Future由运行在forkjoinpool.commonpool()中的任务异步完成,其值通过调用给定的供应商获得。
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/completablefuture.html#supplyasync-java.util.function.supplier供应商-
下面是代码中相同工作流的简化版本。

  1. List<CompletableFuture<Integer>> futures = new ArrayList<>();
  2. for (int i = 0; i < 21; i++) {
  3. int finalI = i;
  4. futures.add(CompletableFuture.supplyAsync(() -> {
  5. System.out.println(finalI);
  6. return 3;
  7. }));
  8. }
  9. System.out.println("before running all of");
  10. CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[3]));
  11. try {
  12. allOf.get();
  13. } catch (InterruptedException | ExecutionException e) {
  14. e.printStackTrace();
  15. }

输出显示线程正在异步运行。它不是一个接一个的。

  1. before running all of
  2. 0
  3. 1
  4. 2
  5. 5
  6. 3
  7. 9
  8. 10
  9. 4
  10. 14
  11. 11
  12. 16
  13. 6
  14. 15
  15. 13
  16. 12
  17. 20
  18. 7
  19. 19
  20. 18
  21. 17
  22. 8
展开查看全部

相关问题