feat: 多线程1

This commit is contained in:
2024-02-28 20:47:13 +08:00
parent e13b3f7c2d
commit 321849b4cb
3 changed files with 124 additions and 20 deletions

View File

@@ -0,0 +1,47 @@
package com.landaiqing.subject.domain.config;
import org.apache.commons.lang3.StringUtils;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 自定义名称的线程工厂
*
* @author: landaiqing
* @date: 2024/2/18
*/
public class CustomNameThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
CustomNameThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
if (StringUtils.isBlank(name)) {
name = "pool";
}
namePrefix = name + "-" +
poolNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon()){
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY){
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}

View File

@@ -0,0 +1,27 @@
package com.landaiqing.subject.domain.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池的config管理
*
* @author: landaiqing
* @date: 2024/2/18
*/
@Configuration
public class ThreadPoolConfig {
@Bean(name = "labelThreadPool")
public ThreadPoolExecutor getLabelThreadPool() {
return new ThreadPoolExecutor(20, 100, 5,
TimeUnit.SECONDS, new LinkedBlockingDeque<>(40),
new CustomNameThreadFactory("label"),
new ThreadPoolExecutor.CallerRunsPolicy());
}
}

View File

@@ -3,6 +3,7 @@ package com.landaiqing.subject.domain.service.impl;
import com.alibaba.fastjson.JSON;
import com.landaiqing.subject.common.enums.CategoryTypeEnum;
import com.landaiqing.subject.common.enums.IsDeletedFlagEnum;
import com.landaiqing.subject.domain.config.ThreadPoolConfig;
import com.landaiqing.subject.domain.convert.SubjectCategoryConverter;
import com.landaiqing.subject.domain.entity.SubjectCategoryBO;
import com.landaiqing.subject.domain.entity.SubjectLabelBO;
@@ -13,13 +14,18 @@ import com.landaiqing.subject.infra.basic.entity.SubjectMapping;
import com.landaiqing.subject.infra.basic.service.SubjectCategoryService;
import com.landaiqing.subject.infra.basic.service.SubjectLabelService;
import com.landaiqing.subject.infra.basic.service.SubjectMappingService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -34,6 +40,9 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
@Resource
private SubjectLabelService subjectLabelService;
@Resource
private ThreadPoolExecutor labelThreadPool;
/**
* @description: 新增分类
* @param: [subjectCategoryBO]
@@ -113,6 +122,7 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
* @author landaiqing
* @date: 2024/2/26 20:46
*/
@SneakyThrows
@Override
public List<SubjectCategoryBO> queryCategoryAndLabel(SubjectCategoryBO subjectCategoryBO) {
// 查询当前分类下的所以的分类
@@ -126,28 +136,48 @@ public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainSe
}
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 -> {
SubjectMapping subjectMapping = new SubjectMapping();
subjectMapping.setCategoryId(category.getId());
List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
if (CollectionUtils.isEmpty(mappingList)) {
return;
}
List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
List<SubjectLabelBO> labelBOList = new LinkedList<>();
subjectLabelList.forEach(label -> {
SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
subjectLabelBO.setId(label.getId());
subjectLabelBO.setLabelName(label.getLabelName());
subjectLabelBO.setCategoryId(label.getCategoryId());
subjectLabelBO.setSortNum(label.getSortNum());
labelBOList.add(subjectLabelBO);
});
category.setLabelBOList(labelBOList);
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()));
});
return categoryBOList;
}
private Map<Long, List<SubjectLabelBO>> getLabelBOList(SubjectCategoryBO category) {
Map<Long, List<SubjectLabelBO>> labelMap = new HashMap<>();
SubjectMapping subjectMapping = new SubjectMapping();
subjectMapping.setCategoryId(category.getId());
List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
if (CollectionUtils.isEmpty(mappingList)) {
return null;
}
List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
List<SubjectLabelBO> labelBOList = new LinkedList<>();
subjectLabelList.forEach(label -> {
SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
subjectLabelBO.setId(label.getId());
subjectLabelBO.setLabelName(label.getLabelName());
subjectLabelBO.setCategoryId(label.getCategoryId());
subjectLabelBO.setSortNum(label.getSortNum());
labelBOList.add(subjectLabelBO);
});
labelMap.put(category.getId(), labelBOList);
return labelMap;
}
}