feat: 多线程2
This commit is contained in:
@@ -24,6 +24,8 @@ import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -135,23 +137,35 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
|
||||
, JSON.toJSONString(subjectCategoryList));
|
||||
}
|
||||
List<SubjectCategoryBO> categoryBOList = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryList);
|
||||
// 一次获取标签信息
|
||||
List<FutureTask<Map<Long, List<SubjectLabelBO>>>> futureTaskList = new LinkedList<>();
|
||||
Map<Long, List<SubjectLabelBO>> map = new HashMap<>();
|
||||
//线程池并发调用
|
||||
categoryBOList.forEach(category -> {
|
||||
FutureTask<Map<Long, List<SubjectLabelBO>>> futureTask = new FutureTask<>(() ->
|
||||
getLabelBOList(category));
|
||||
futureTaskList.add(futureTask);
|
||||
labelThreadPool.submit(futureTask);
|
||||
});
|
||||
for (FutureTask<Map<Long, List<SubjectLabelBO>>> futureTask : futureTaskList) {
|
||||
Map<Long, List<SubjectLabelBO>> resultMap = futureTask.get();
|
||||
if (CollectionUtils.isEmpty(resultMap)) {
|
||||
continue;
|
||||
List<CompletableFuture<Map<Long, List<SubjectLabelBO>>>> completableFutureList = categoryBOList.stream().map(category ->
|
||||
CompletableFuture.supplyAsync(() -> getLabelBOList(category), labelThreadPool)
|
||||
).collect(Collectors.toList());
|
||||
completableFutureList.forEach(future -> {
|
||||
try {
|
||||
Map<Long, List<SubjectLabelBO>> resultMap = future.get();
|
||||
map.putAll(resultMap);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
map.putAll(resultMap);
|
||||
}
|
||||
});
|
||||
// // 一次获取标签信息
|
||||
// List<FutureTask<Map<Long, List<SubjectLabelBO>>>> futureTaskList = new LinkedList<>();
|
||||
// Map<Long, List<SubjectLabelBO>> map = new HashMap<>();
|
||||
// //线程池并发调用
|
||||
// categoryBOList.forEach(category -> {
|
||||
// FutureTask<Map<Long, List<SubjectLabelBO>>> futureTask = new FutureTask<>(() ->
|
||||
// getLabelBOList(category));
|
||||
// futureTaskList.add(futureTask);
|
||||
// labelThreadPool.submit(futureTask);
|
||||
// });
|
||||
// for (FutureTask<Map<Long, List<SubjectLabelBO>>> futureTask : futureTaskList) {
|
||||
// Map<Long, List<SubjectLabelBO>> resultMap = futureTask.get();
|
||||
// if (CollectionUtils.isEmpty(resultMap)) {
|
||||
// continue;
|
||||
// }
|
||||
// map.putAll(resultMap);
|
||||
// }
|
||||
categoryBOList.forEach(categoryBO -> {
|
||||
categoryBO.setLabelBOList(map.get(categoryBO.getId()));
|
||||
});
|
||||
@@ -159,6 +173,9 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
|
||||
}
|
||||
|
||||
private Map<Long, List<SubjectLabelBO>> getLabelBOList(SubjectCategoryBO category) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("getLabelBOList:{}", JSON.toJSONString(category));
|
||||
}
|
||||
Map<Long, List<SubjectLabelBO>> labelMap = new HashMap<>();
|
||||
SubjectMapping subjectMapping = new SubjectMapping();
|
||||
subjectMapping.setCategoryId(category.getId());
|
||||
@@ -167,9 +184,9 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
|
||||
return null;
|
||||
}
|
||||
List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
|
||||
List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
|
||||
List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
|
||||
List<SubjectLabelBO> labelBOList = new LinkedList<>();
|
||||
subjectLabelList.forEach(label -> {
|
||||
labelList.forEach(label -> {
|
||||
SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
|
||||
subjectLabelBO.setId(label.getId());
|
||||
subjectLabelBO.setLabelName(label.getLabelName());
|
||||
|
Reference in New Issue
Block a user