多线程实现数据批量处理代返回值
Map<String, String> map = new HashMap<>();
ExecutorService executorService = Executors.newFixedThreadPool(20);
Callable<List<Map<String, String>>> task = null;
List<Callable<List<Map<String, String>>>> clist = new ArrayList<>();
for (StudentExcelSchool excelSchool : studentExcelSchool) {
//添加单个线程
task = new Callable<List<Map<String, String>>>() {
@Override
public List<Map<String, String>> call() throws Exception {
//处理业务数据
List<Map<String, String>> maps = saveStudentBatch(excelSchool, schoolById, loginUser);
return maps;
}
};
clist.add(task);
}
try {
List<Future<List<Map<String, String>>>> futures = executorService.invokeAll(clist);
for (Future<List<Map<String, String>>> future : futures) {
error.addAll(future.get());
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
map.put("报错", e.getMessage());
error.add(map);
} finally {
//处理业务数据
String id = MapUtils.getString(result.getData(), "id");
if(StringUtil.isNotBlank(id)){
JsonResult<List<String>> userIdFormSysUserRoleByRoleId = userFeign.findUserIdFormSysUserRoleByRoleId(id);
if(userIdFormSysUserRoleByRoleId.getData().size() == 0){
userFeign.deleteRole(Long.valueOf(id));
}
}
executorService.shutdown();
}
return error;