//Executors创建线程池new固定的10个线程
ExecutorService taskExecutor = Executors.newFixedThreadPool(5);
final CountDownLatch latch = new CountDownLatch(userInfoList.size());//用于判断所有的线程是否结束
for (SysUserVO userVO : userInfoList) {
Runnable run = new Runnable() {
public void run() {
try {
if (!userVO.getIsDeleted()) {
//查询用户有权限的项目
AppResponse response = userProjService.authorizedProjects(null,userVO.getUserCode());
List<String> projectIds =response.getData()!=null? ObjectUtil.castList(response.getData(),String.class) :null;
// 查询用户已授权的角色
List<RoleDTO> checkedRoleList = roleDao.listUserRoles(userVO.getUserCode());
String roleCodes = checkedRoleList.stream().map(item -> item.getRoleCode()).collect(Collectors.joining(","));
String roleNames = checkedRoleList.stream().map(item -> item.getRoleName()).collect(Collectors.joining(","));
//projectService.getProjectByIds 这个方法是远程调用,144多个远程调用。。。改成一次远程调用,并且for循环改成多现场优化
if(CollectionUtils.isNotEmpty(projectIds)) {
List<ProjectDTO> projectDTOS = allProject.stream().filter(project -> projectIds.contains(project.getId())).distinct().collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(projectDTOS)) {
String projectNames = projectDTOS.stream().map(item -> item.getProjectName()).collect(Collectors.joining(","));
userVO.setProjectNameList(projectNames);
}
// userVO.setProjectIdList(String.join(",",projectIds));
// AppResponse projects = projectService.getProjectByIds(String.join(",",projectIds));
// if(projects==null){
// continue;
// }
// if(projects.getData()!=null){
// List<ProjectDTO> projectDTOs = JSON.parseArray(JSON.toJSONString(projects.getData()),
// ProjectDTO.class);
// String projectNames=projectDTOs.stream().map(item -> item.getProjectName()).collect(Collectors.joining(","));
// userVO.setProjectNameList(projectNames);
// }
}
userVO.setRoleList(roleCodes);
userVO.setRoleNameList(roleNames);
}
} finally {
latch.countDown(); //每次调用CountDown(),计数减1
}
}
};
taskExecutor.execute(run);//开启线程执行池中的任务。还有一个方法submit也可以做到,它的功能是提交指定的任务去执行并且返回Future对象,即执行的结果
}
try {
//等待所有线程执行完毕
latch.await(); //主程序执行到await()函数会阻塞等待线程的执行,直到计数为0
} catch (InterruptedException e) {
e.printStackTrace();
}
taskExecutor.shutdown();//关闭线程池
//所有线程执行完毕,执行主线程
// for (SysUserVO userVO : userInfoList) {
// if (userVO.getIsDeleted()) {
// continue;
// }
// //查询用户有权限的项目
// AppResponse response = userProjService.authorizedProjects(null,userVO.getUserCode());
// List<String> projectIds =response.getData()!=null? ObjectUtil.castList(response.getData(),String.class) :null;
// // 查询用户已授权的角色
// List<RoleDTO> checkedRoleList = roleDao.listUserRoles(userVO.getUserCode());
// String roleCodes = checkedRoleList.stream().map(item -> item.getRoleCode()).collect(Collectors.joining(","));
// String roleNames = checkedRoleList.stream().map(item -> item.getRoleName()).collect(Collectors.joining(","));
// //TODO projectService.getProjectByIds 这个方法是远程调用,144多个远程调用。。。改成一次远程调用,并且for循环改成多现场优化
// if(CollectionUtils.isNotEmpty(projectIds)) {
// List<ProjectDTO> projectDTOS = allProject.stream().filter(project -> projectIds.contains(project.getId())).distinct().collect(Collectors.toList());
// if (CollectionUtils.isNotEmpty(projectDTOS)) {
// String projectNames = projectDTOS.stream().map(item -> item.getProjectName()).collect(Collectors.joining(","));
// userVO.setProjectNameList(projectNames);
// }
userVO.setProjectIdList(String.join(",",projectIds));
AppResponse projects = projectService.getProjectByIds(String.join(",",projectIds));
if(projects==null){
continue;
}
if(projects.getData()!=null){
List<ProjectDTO> projectDTOs = JSON.parseArray(JSON.toJSONString(projects.getData()),
ProjectDTO.class);
String projectNames=projectDTOs.stream().map(item -> item.getProjectName()).collect(Collectors.joining(","));
userVO.setProjectNameList(projectNames);
}
//
// }
// userVO.setRoleList(roleCodes);
// userVO.setRoleNameList(roleNames);
// }
将for循环改为多线程,最好用newFixedThreadPool,不消耗内存
并且
try {
//等待所有线程执行完毕
latch.await(); //主程序执行到await()函数会阻塞等待线程的执行,直到计数为0
} catch (InterruptedException e) {
e.printStackTrace();
}
taskExecutor.shutdown();//关闭线程池
这句等待线程池用完之后关闭
这样优化之后原本的20s请求时间变为了300多ms
修改前:
修改后: