java~ForkJoinPool分而致之处理大数据

ForkJoinPool的思想,是将大的集合进行拆分,计算处理之后,再把结果合并,这体现了多核时代的并行计算能力。

集合拆分成元素

  List<integer> maps = Lists.newArrayList();
  int count = 100;
  for (int i = 0; i < count; i++) {
      maps.add(i);
  }
  StopWatch stopWatch = new StopWatch();
  stopWatch.start();
  List<integer> odds = new ArrayList<>();
  ForkJoinPool forkJoinPool = new ForkJoinPool(10);
  forkJoinPool.submit(() -> {
      maps.parallelStream()
              .forEach(map -> {
                  if (map % 2 == 0) {
                      odds.add(map);
                      try {
                          Thread.sleep(1);
                      } catch (InterruptedException e) {
                          throw new RuntimeException(e);
                      }
                  }
              });

  }).join();
  stopWatch.stop();
  logger.info(stopWatch.prettyPrint());
</integer></integer>

大集合拆分成集合

ListUtil.split使用了hutool框架,5.4.5版本提供了集合拆分功能

//fork&#x96C6;&#x5408;
List<list<integer>> jihe = new ArrayList<>();
jihe = ListUtil.split(maps, 9);
List<list<integer>> finalJihe = jihe;
forkJoinPool.submit(() -> {
    finalJihe.parallelStream()
            .forEach(map -> {
                logger.info("map size:{}", map.size());
            });
}).join();
</list<integer></list<integer>

Original: https://www.cnblogs.com/lori/p/16532665.html
Author: 张占岭
Title: java~ForkJoinPool分而致之处理大数据

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/576076/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球