// 线程池大小
public static final int THREAD_POLL_SIZE = 4;
// 获取固定大小线程池
private static ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POLL_SIZE);
private Map<String, List<String>> batchXXProcess(List<Object> objList)throws Exception{
Map<String, List<String>> result = new HashMap<String, List<String>>();
List<OperationResult> operationResults = new ArrayList<OperationResult>();
// 获取子任务
List<Callable<Object>> tasks = getBatchXXTasks(objList, operationResults);
executorService.invokeAll(tasks);
List<String> successfulList = new ArrayList<String>();
List<String> failedList = new ArrayList<String>();
generateOperationResult(successfulList, failedList, operationResults);
result.put("succ", successfulList);
result.put("fail", failedList);
return result;
}
private List<Callable<Object>> getBatchXXTasks(List<Object> objList, List<OperationResult> operationResults) throws Exception{
List<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
for (Object obj : objList){
if (null == obj){
continue;
}
XXThread thread = new XXThread(obj);
tasks.add(thread);
operationResults.add(thread.getOperationResult());
}
return tasks;
}
private void generateOperationResult(List<String> successfulList,List<String> failedList, List<OperationResult> operationResults){
for (OperationResult operationResult : operationResults){
if (null == operationResult){
continue;
}
boolean isSuccess = operationResult.isSuccess();
String name = operationResult.getName();
if (isSuccess){
successfulList.add(name);
}else{
failedList.add(name);
}
}
}