private Map<String, ExamAnswerItemV1Entity> getSuperItemMapV3(Set<String> examId, Long schoolId) {
List<String> examIdList = Lists.newArrayList(examId);
ExamSuperItemServiceImpl.ItemAnswerQueryTask task = new ExamSuperItemServiceImpl.ItemAnswerQueryTask(examIdList, schoolId);
Map<String, ExamAnswerItemV1Entity> superItemMap ;
try {
superItemMap = forkjoinPool.submit(task).get();
} catch (InterruptedException | ExecutionException e) {
logger.error("camp statistics task error", e);
throw new ExamException("服务器错误");
}
return superItemMap;
}
private ForkJoinPool forkjoinPool = new ForkJoinPool(10, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
(t, e) -> {
logger.error("camp statistics async thread error {} {}", t, e);
}, true);
private static class ItemAnswerQueryTask extends RecursiveTask<Map<String, ExamAnswerItemV1Entity>> {
private List<String> examId;
private Long schoolId;
private static final Integer THRESLOD = 500;
ItemAnswerQueryTask(List<String> examIds,Long schoolId) {
this.examId = examIds;
this.schoolId = schoolId;
}
@Override
protected Map<String, ExamAnswerItemV1Entity> compute() {
int size = examId.size();
if (size == 0) {
return new HashMap<>();
}
if (size <= THRESLOD) {
Map<String, ExamAnswerItemV1Entity> result = new ConcurrentHashMap<String, ExamAnswerItemV1Entity>();
ItemBaseInfoListReq req = new ItemBaseInfoListReq();
req.setSuperIdList(examId);
req.setSchoolId(schoolId == null ? null : schoolId.toString());
List<SuperItemInfo4LoadMagicResp> superItemInfo4LoadMagicResps = itemClient.getSuperItemEntityInfoListV1(req);
superItemInfo4LoadMagicResps.forEach(entity -> {
result.put(entity.getSuperItemId(), new ExamAnswerItemV1Entity(entity.getSuperItemId(),entity));
});
return result;
} else {
int mid = size / 2;
List<String> left = examId.subList(0, mid);
List<String> right = examId.subList(mid, size);
ExamSuperItemServiceImpl.ItemAnswerQueryTask leftTask = new ExamSuperItemServiceImpl.ItemAnswerQueryTask(left, schoolId);
ExamSuperItemServiceImpl.ItemAnswerQueryTask rightTask = new ExamSuperItemServiceImpl.ItemAnswerQueryTask(right, schoolId);
leftTask.fork();
rightTask.fork();
Map<String, ExamAnswerItemV1Entity> allExam = new ConcurrentHashMap<>(leftTask.join());
allExam.putAll(rightTask.join());
return allExam;
}
}
}