long startTime = System.currentTimeMillis();
System.out.println("开始查询-->");
List<Callable<HashMap<String, List<SiteBaseInfo>>>> taskList = new ArrayList<>();//创建任务
DataScope dataScope = new DataScope();
dataScope.setDeptCode(siteBaseInfo.getDeptCode());
dataScope.setIsDeptCode(true);
String deptCode = siteBaseInfo.getDeptCode();
List<String> strings = BeanValueTrimUtil.changeStringToList(deptCode);
for (String s : strings) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Callable< HashMap<String, List<SiteBaseInfo>>> task = new Callable<HashMap<String, List<SiteBaseInfo>>>() {
@Override
public HashMap<String, List<SiteBaseInfo>> call() throws Exception {
SiteBaseInfo siteBaseInfo1 = new SiteBaseInfo();
siteBaseInfo1.setDeptCode(s);
RequestContextHolder.setRequestAttributes(requestAttributes, true);
HashMap<String, List<SiteBaseInfo>> stringObjectHashMap = new HashMap<>();
try {
DataScope dataScope = new DataScope();
siteBaseInfo.setDeleteFlag("0");
siteBaseInfo.setIsLogicalFlag("0");
if (StringUtils.isNotBlank(siteBaseInfo.getDeptCode())) {
dataScope.setDeptCode(siteBaseInfo.getDeptCode());
siteBaseInfo.setDeptCode(null);
} else if (SecurityUtils.getUser() != null) {
R treeR = myRemoteDeptService.tree(null, null, null);
List<Map<String, Object>> s = (List<Map<String, Object>>) treeR.getData();
dataScope.setDeptCode((String) s.get(0).get("id"));
}
dataScope.setIsDeptCode(true);
QueryWrapper<SiteBaseInfo> siteBaseInfoQueryWrapper = new QueryWrapper<>(siteBaseInfo);
LambdaQueryWrapper<SiteBaseInfo> lambda = siteBaseInfoQueryWrapper.lambda();
lambda.eq(SiteBaseInfo::getDeleteFlag, "0");
lambda.eq(SiteBaseInfo::getIsLogicalFlag, "0");
if (StringUtils.isNotBlank(siteBaseInfo.getSiteName())) {
lambda.like(SiteBaseInfo::getSiteName, siteBaseInfo.getSiteName());
}
List<SiteBaseInfo> siteBaseInfos = siteBaseInfoMapper.selectSiteBaseInfoListNoPage2(siteBaseInfoQueryWrapper,s);
stringObjectHashMap.put(s,siteBaseInfos);
return stringObjectHashMap;
} catch (Exception e) {
e.printStackTrace();
log.error("分页查询超时page" + s);
return null;
}
}
};//条件查询,name没有就给null
taskList.add(task);
}
int threadNum = 10;//可自定义线程数量
ExecutorService executor = Executors.newFixedThreadPool(threadNum);
List<Future<HashMap<String, List<SiteBaseInfo>>>> futureList = executor.invokeAll(taskList);
if (futureList != null && futureList.size() > 0) {
for (Future<HashMap<String, List<SiteBaseInfo>>> future : futureList) {
if (future.get() != null) {
// list.addAll(future.get());
HashMap<String, List<SiteBaseInfo>> stringListHashMap = future.get();
returnMap.putAll(stringListHashMap);
}
}
}
executor.shutdown();//关闭线程
long endTime = System.currentTimeMillis();
long s = ((endTime - startTime) / 1000);
System.out.println(threadNum + "个线程查询花了" + s + "秒");
} catch (Exception e) {
log.error("siteBaseInfoPage{}", e);
}
return R.ok(returnMap);
第二种
try {
List<FutureTask<Map<String, Object>>> resultList = new ArrayList<>();
for (SysDept d : subDeptList) {
FutureTask<Map<String, Object>> futureTask = new FutureTask<>(new myTask(baseMapper, d));
executorService.submit(futureTask);
//future异步模式,把任务放进去先,先不取结果
resultList.add(futureTask);
}
resultList.forEach(e -> {
try {
result.add(e.get());
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
}
});
} catch (Exception e) {
e.printStackTrace();
executorService.shutdown();
return R.ok();
}
executorService.shutdown();
return R.ok(result);
@Slf4j
class myTask implements Callable<Map<String, Object>> {
private final SiteBaseInfoMapper mapper;
private final SysDept dept;
public myTask(SiteBaseInfoMapper mapper, SysDept dept) {
this.mapper = mapper;
this.dept = dept;
}
@Override
public Map<String, Object> call() throws Exception {
long s = System.currentTimeMillis();
log.info("====================" + dept.getName() + "开始=====================================");
//查询组织机构下级所有组织机构编码
long t = System.currentTimeMillis();
// List<KeyDevBaseInfoDto> keyDevBaseInfoDtoList = mapper.getDevSummaryByDevClassOfALevelByCodes(dept.getDeptCode());
List<Map> keyDevBaseInfoDtoList = mapper.getSiteBaseInfoStatisticsByBusinessTypeCodes2(dept.getDeptCode(),"true");
List<Map> aFalse = mapper.getSiteBaseInfoStatisticsByBusinessTypeCodes2(dept.getDeptCode(), "false");
keyDevBaseInfoDtoList.addAll(aFalse);
long ed = System.currentTimeMillis();
log.info("======================" + dept.getName() + "查询数据库耗时:" + (ed - t) / 1000);
Map<String, Object> dtoMap = new HashMap<>();
dtoMap.put("111", 0);
dtoMap.put("222", 0);
dtoMap.put("333", 0);
dtoMap.put("444", 0);
dtoMap.put("555", 0);
dtoMap.put("666", 0);
for (Map map : keyDevBaseInfoDtoList) {
String dtoMapKey = (String) map.get("business_type_codes") +map.get("site_type") ;
dtoMap.put(dtoMapKey, (Long) map.get("quantity"));
}
dtoMap.put("deptName", ObjectUtil.isNotEmpty(dept.getShortName()) ? dept.getShortName() : dept.getName());
List<String> sortList = Arrays.asList("1", "2", "3", "4", "5","6");
Map sortedMap = dtoMap.entrySet().stream()
.sorted(Comparator.comparing(entry -> sortList.indexOf(entry.getKey())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
long o = System.currentTimeMillis();
log.info("=========================" + dept.getName() + "总耗时:" + (o - s) / 1000);
return sortedMap;
}
}