parallelStream 使用自定义 ForkJoinPool防止任务堆积

This commit is contained in:
wangliwen 2022-08-12 14:11:34 +08:00
parent 8980f61b1c
commit f45e3ea2f4
1 changed files with 50 additions and 61 deletions

View File

@ -474,14 +474,6 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
).limit(pageSize).collect(Collectors.toList()); ).limit(pageSize).collect(Collectors.toList());
return temp; return temp;
}).get(); }).get();
// ids = selectDTOPageSpecilTotal.parallelStream().map(Map.class::cast).sorted(Comparator.comparing(x -> {
// Map index = (Map) x;
// String string = (index.get("total") == null) ? "0" : index.get("total").toString();
// return Long.valueOf(string);
// }
// ).reversed()).skip((long) (pageNum - 1) * pageSize).limit(pageSize).map(x ->
// Long.valueOf(x.get("id").toString())
// ).limit(pageSize).collect(Collectors.toList());
break; break;
case "ASC": // total 升序 case "ASC": // total 升序
ids = customThreadPool.submit(() -> { ids = customThreadPool.submit(() -> {
@ -494,13 +486,6 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
).limit(pageSize).collect(Collectors.toList()); ).limit(pageSize).collect(Collectors.toList());
return temp; return temp;
}).get(); }).get();
// ids = selectDTOPageSpecilTotal.parallelStream().map(Map.class::cast).sorted(Comparator.comparing(x -> {
// String string = (x.get("total") == null) ? "0" : x.get("total").toString();
// return Long.valueOf(string);
// }
// )).skip((pageNum - 1) * pageSize).limit(pageSize).map(x ->
// Long.valueOf(x.get("id").toString())
// ).limit(pageSize).collect(Collectors.toList());
break; break;
} }
resourceDTOS = resourceDao.selectDTOPage(resourceDTO, null, null, null, null, ids); resourceDTOS = resourceDao.selectDTOPage(resourceDTO, null, null, null, null, ids);
@ -1131,6 +1116,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
public void KnowledgeBase() { public void KnowledgeBase() {
final List<String> knowledgeUUID = jdbcTemplate.queryForList("SELECT note1 FROM tb_data_resource WHERE type ='知识库' AND note1 IS NOT NULL FOR UPDATE;", String.class).stream().distinct().collect(Collectors.toList()); final List<String> knowledgeUUID = jdbcTemplate.queryForList("SELECT note1 FROM tb_data_resource WHERE type ='知识库' AND note1 IS NOT NULL FOR UPDATE;", String.class).stream().distinct().collect(Collectors.toList());
final int pageSize = CPU_NUM * 10; final int pageSize = CPU_NUM * 10;
ForkJoinPool customThreadPool = new ForkJoinPool(CPU_NUM * 3);
Arrays.stream(catalogIds).map(index -> { Arrays.stream(catalogIds).map(index -> {
logger.info("处理:{}", index); logger.info("处理:{}", index);
CopyOnWriteArrayList<CompletableFuture> task = new CopyOnWriteArrayList<>(); CopyOnWriteArrayList<CompletableFuture> task = new CopyOnWriteArrayList<>();
@ -1208,6 +1194,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
if (list.isEmpty()) { if (list.isEmpty()) {
end.set(false); end.set(false);
} }
customThreadPool.submit(() -> {
list.parallelStream().filter(resource -> { list.parallelStream().filter(resource -> {
Map<String, Object> map = (Map<String, Object>) resource; Map<String, Object> map = (Map<String, Object>) resource;
return !knowledgeUUID.contains(map.get("uuid").toString()); return !knowledgeUUID.contains(map.get("uuid").toString());
@ -1256,6 +1243,8 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
this.insertWithAttrs(dto); this.insertWithAttrs(dto);
logger.info("插入:{}", dto.getName()); logger.info("插入:{}", dto.getName());
}); });
}).join();
customThreadPool.shutdown();
}, executor)); }, executor));
try { try {
Thread.sleep(300L); Thread.sleep(300L);