List<TomEmp> empList = tomEmpMapper.selectAllEmp();
logger.info("开始循环人员集合");
int threadSize = 1000;//每1000条数据开启一个线程
int remainder = empList.size()%threadSize;
int threadNum = 0;//线程数
if(remainder == 0){
threadNum = empList.size()/threadSize;
} else {
threadNum = empList.size()/threadSize + 1;
}
ExecutorService eService = Executors.newFixedThreadPool(threadNum );//创建一个线程池
List<Callable<List<StudentStatistics>>> cList = new ArrayList<>();
Callable<List<StudentStatistics>> task = null;
List<TomEmp> sList = null;
for(int i=0;i<threadNum;i++){
if(i == threadNum - 1){
sList = empList.subList(i*threadSize, empList.size());
} else {
sList = empList.subList(i*threadSize, (i+1)*threadSize);
}
final List<TomEmp> nowList = sList;
task = new Callable<List<StudentStatistics>>() {
@Override
public List<StudentStatistics> call() throws Exception {
List<StudentStatistics> studentStaticList =new ArrayList<>();
studentStaticList=convertData(nowList,studentStaticList);
return studentStaticList;
}
private List<StudentStatistics> convertData(List<TomEmp> nowList,List<StudentStatistics> studentStaticList) {
for(TomEmp emp : nowList) {
if(emp.getName().length()>50) {
logger.error("名字超长为"+emp.getName());
continue;
}
paramMap.clear();
paramMap.put("code", emp.getCode());
List<StudentStatistics> studentList = studentInfoMapper.selectStudentData(paramMap);
for(StudentStatistics model : studentList) {
model.setOrgBeginDate(emp.getOrgbegindate());
model.setPostSeriesName(emp.getPostseriesname());
model.setCityName(emp.getCityname());
model.setPostStat(emp.getPoststat());
model.setDeptCode(emp.getDeptcode());
model.setName(emp.getName());
try {
//封装一级部门到五级部门
if(model.getDeptTopCode()!=null) {
List<DeptInfo> deptInfo = currencyService.selectTopDepts(model.getDeptTopCode());
for(int j=1;j<=deptInfo.size();j++) {
if(j>5) break;
if(j==1) {
model.setFirstDeptName(deptInfo.get(deptInfo.size()-1).getDeptName());
}else if(j==2) {
model.setSecondDeptName(deptInfo.get(deptInfo.size()-2).getDeptName());
}else if(j==3) {
model.setThirdDeptName(deptInfo.get(deptInfo.size()-3).getDeptName());
}else if(j==4) {
model.setFourDeptName(deptInfo.get(deptInfo.size()-4).getDeptName());
}else if(j==5) {
model.setFiveDeptName(deptInfo.get(deptInfo.size()-5).getDeptName());
}
}
}
//如果是活动中的考试、课程则查询相关活动信息
if(model.getTaskType().equals(ConstantsUtil.STUDENT_ACTIVITY_EXAM)||
model.getTaskType().equals(ConstantsUtil.STUDENT_ACTIVITY_COURSE)) {
paramMap.put("activityId", Integer.parseInt(model.getActivityId()));
paramMap.put("code", model.getCode());
StudentStatistics student = studentInfoMapper.selectActivityInfo(paramMap);
if(student!=null) {
model.setActivityNumber(student.getActivityNumber());
model.setActivityName(student.getActivityName());
model.setActivityStatus(student.getActivityStatus());
}
}
}catch(Exception e) {
logger.error("同步学员档案异常,工号为:"+model.getCode());
continue;
}
}
studentStaticList.addAll(studentList);
}
return studentStaticList;
}
};
cList.add(task);
}
List<Future<List<StudentStatistics>>> results = eService.invokeAll(cList);
for(int j=0;j<results.size();j++){
saveList.addAll(results.get(j).get());
}
//保存学员档案
jdbcService.batchInsertStudentInfo(saveList);
eService.shutdown();
参考:https://blog.youkuaiyun.com/java_chegnxuyuan/article/details/90081277