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());
大集合拆分成集合
ListUtil.split使用了hutool框架,5.4.5版本提供了集合拆分功能
//fork集合
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();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2020-07-29 springcloud~微服务里的oauth2集成总结
2015-07-29 我心中的核心组件(可插拔的AOP)~分布式Session组件
2012-07-29 不说技术~有时,开发者还是应该讲究一点!
2011-07-29 面向对象~程序应该具有可维护性,代码可复用性,可扩展性和灵活性