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