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());
return temp;
}).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;
case "ASC": // total 升序
ids = customThreadPool.submit(() -> {
@ -494,13 +486,6 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
).limit(pageSize).collect(Collectors.toList());
return temp;
}).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;
}
resourceDTOS = resourceDao.selectDTOPage(resourceDTO, null, null, null, null, ids);
@ -1131,6 +1116,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
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 int pageSize = CPU_NUM * 10;
ForkJoinPool customThreadPool = new ForkJoinPool(CPU_NUM * 3);
Arrays.stream(catalogIds).map(index -> {
logger.info("处理:{}", index);
CopyOnWriteArrayList<CompletableFuture> task = new CopyOnWriteArrayList<>();
@ -1208,54 +1194,57 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
if (list.isEmpty()) {
end.set(false);
}
list.parallelStream().filter(resource -> {
Map<String, Object> map = (Map<String, Object>) resource;
return !knowledgeUUID.contains(map.get("uuid").toString());
}).forEach(resource -> {
Map<String, Object> map = (Map<String, Object>) resource;
ResourceDTO dto = new ResourceDTO();
dto.setName(map.get("title").toString());
dto.setType("知识库");
dto.setVisits(0L);
//所属部门暂时设为青岛市政府办公厅
dto.setDeptId(1517116100113850370L);
dto.setNote1(map.get("uuid").toString());
dto.setDelFlag(0);
ArrayList<AttrEntity> infoList = new ArrayList<>();
map.forEach((key, value) -> {
switch (key) {
case "title":
dto.setName(value.toString());
break;
case "url":
dto.setLink(value.toString());
break;
case "createtime":
Date createDate = new Date(Long.parseLong(value.toString()));
dto.setCreateDate(createDate);
break;
default:
AttrEntity attrEntity = new AttrEntity();
attrEntity.setDelFlag(0);
attrEntity.setAttrType(key);
attrEntity.setAttrValue(value.toString());
infoList.add(attrEntity);
break;
customThreadPool.submit(() -> {
list.parallelStream().filter(resource -> {
Map<String, Object> map = (Map<String, Object>) resource;
return !knowledgeUUID.contains(map.get("uuid").toString());
}).forEach(resource -> {
Map<String, Object> map = (Map<String, Object>) resource;
ResourceDTO dto = new ResourceDTO();
dto.setName(map.get("title").toString());
dto.setType("知识库");
dto.setVisits(0L);
//所属部门暂时设为青岛市政府办公厅
dto.setDeptId(1517116100113850370L);
dto.setNote1(map.get("uuid").toString());
dto.setDelFlag(0);
ArrayList<AttrEntity> infoList = new ArrayList<>();
map.forEach((key, value) -> {
switch (key) {
case "title":
dto.setName(value.toString());
break;
case "url":
dto.setLink(value.toString());
break;
case "createtime":
Date createDate = new Date(Long.parseLong(value.toString()));
dto.setCreateDate(createDate);
break;
default:
AttrEntity attrEntity = new AttrEntity();
attrEntity.setDelFlag(0);
attrEntity.setAttrType(key);
attrEntity.setAttrValue(value.toString());
infoList.add(attrEntity);
break;
}
});
AttrEntity attrEntity = new AttrEntity();
attrEntity.setDelFlag(0);
attrEntity.setAttrType("文件类型");
if ("f49561afc7204f008c4bb3cd821eb6ba".equals(index)) {
attrEntity.setAttrValue("政府公报");
} else {
attrEntity.setAttrValue("政策解读");
}
infoList.add(attrEntity);
dto.setInfoList(infoList);
this.insertWithAttrs(dto);
logger.info("插入:{}", dto.getName());
});
AttrEntity attrEntity = new AttrEntity();
attrEntity.setDelFlag(0);
attrEntity.setAttrType("文件类型");
if ("f49561afc7204f008c4bb3cd821eb6ba".equals(index)) {
attrEntity.setAttrValue("政府公报");
} else {
attrEntity.setAttrValue("政策解读");
}
infoList.add(attrEntity);
dto.setInfoList(infoList);
this.insertWithAttrs(dto);
logger.info("插入:{}", dto.getName());
});
}).join();
customThreadPool.shutdown();
}, executor));
try {
Thread.sleep(300L);