結(jié)論
Person有身份證字段:id
List<Person> list
找到list中id=xxx的person
解決:
先將list轉(zhuǎn)為Map<String, Person> key為id,通過map.get(xx)提升性能
1.parallelStream.forEach()
缺點(diǎn):main線程可能不會等待結(jié)束
2.Lists.partition(list, 1000);以1000大小分組
每個(gè)組使用線程池執(zhí)行
優(yōu)點(diǎn):分組執(zhí)行凯亮,每個(gè)線程執(zhí)行各自的私有l(wèi)ist,無鎖的操作代價(jià),最快
3.使用ConcurrentLinkedQueue 共享list味悄,創(chuàng)建n個(gè)線程同時(shí)處理這個(gè)queue
缺點(diǎn):多線程操作同一個(gè)queue,有鎖的判斷代價(jià)
3 demo
log.info("111-1");
//這種有4w條數(shù)據(jù)時(shí)反射啥的巨耗時(shí)
/* List<CompanyRiskWarnVo> warnVos = dataRecords
.stream()
.map(dataRecord -> DataRecordTransform.toBean(dataRecord, CompanyRiskWarnVo.class))
.collect(Collectors.toList());*/
List<CompanyRiskWarnVo> warnVos = new ArrayList<>(dataRecords.size());
//按照上面3分組 ,每組一個(gè)線程執(zhí)行吝秕,統(tǒng)一匯總結(jié)果
List<List<DataRecord>> dataRecordParts = Lists.partition(dataRecords, 1000);
List<Future<List<CompanyRiskWarnVo>>> recordFuture = new ArrayList<>();
for (List<DataRecord> dataRecordPart : dataRecordParts) {
Future<List<CompanyRiskWarnVo>> submit = threadPoolExecutor.submit(new Callable<List<CompanyRiskWarnVo>>() {
@Override
public List<CompanyRiskWarnVo> call() throws Exception {
try {
if (CollectionUtils.isNotEmpty(dataRecordPart)) {
return dataRecordPart.stream().map(dataRecord -> DataRecordTransform.toBean(dataRecord, CompanyRiskWarnVo.class)).collect(Collectors.toList());
}
return Collections.emptyList();
} catch (Exception e) {
return Collections.emptyList();
}
}
});
recordFuture.add(submit);
}
for (Future<List<CompanyRiskWarnVo>> future : recordFuture) {
List<CompanyRiskWarnVo> riskWarnVos = null;
try {
riskWarnVos = future.get();
} catch (Exception e) {
log.warn("get transfer error, ", e);
riskWarnVos = Collections.emptyList();
}
warnVos.addAll(riskWarnVos);
}
stream.parallel
- 底層forkjoin多線程
- main線程會等待多線程結(jié)束
- ArrayList相對linkedList更適合stream遍歷,容易隨機(jī)拆分
[INFO 2022-12-07 10:26:53.907][main][:--]com.xiaomi.scf.risk.web.manager.CommonTest.lambda$parallel$2(CommonTest.java:258)[in parallel]
[INFO 2022-12-07 10:26:53.907][ForkJoinPool.commonPool-worker-1][:--]com.xiaomi.scf.risk.web.manager.CommonTest.lambda$parallel$2(CommonTest.java:258)[in parallel]
[INFO 2022-12-07 10:26:53.907][ForkJoinPool.commonPool-worker-2][:--]com.xiaomi.scf.risk.web.manager.CommonTest.lambda$parallel$2(CommonTest.java:258)[in parallel]
[INFO 2022-12-07 10:26:55.911][ForkJoinPool.commonPool-worker-3][:--]com.xiaomi.scf.risk.web.manager.CommonTest.lambda$parallel$2(CommonTest.java:258)[in parallel]
[INFO 2022-12-07 10:26:55.911][ForkJoinPool.commonPool-worker-5][:--]com.xiaomi.scf.risk.web.manager.CommonTest.lambda$parallel$2(CommonTest.java:258)[in parallel]
[INFO 2022-12-07 10:26:55.911][main][:--]com.xiaomi.scf.risk.web.manager.CommonTest.parallel(CommonTest.java:264)[main end]