List<List<JSONObject>> group_list = CutUtil.groupList(dataList, 10);//暂时改成10(原为1000) List<Callable<List<JSONObject>>> tasks = new ArrayList<Callable<List<JSONObject>>>(); // 添加任务 for(List<JSONObject> info_list : group_list){ counter++; // System.out.println("------------股东穿透开始执行第"+layerNum+"层,第"+counter+"组查询-------------------"); Callable<List<JSONObject>> query_ = new QueryThread(relatedMapper, info_list, layerNum); tasks.add(query_); }
/ 定义固定长度的线程池 防止线程过多 ExecutorService execservice = Executors.newFixedThreadPool(15); List<Future<List<JSONObject>>> futures = execservice.invokeAll(tasks);
// 处理线程返回结果 if (ValidateObject.hasValueInCollection(futures)) { for(Future<List<JSONObject>> future : futures) { dataList2.addAll(future.get()); } } 自定义线程类:
package com.seeyii.util; import java.util.List; import java.util.concurrent.Callable; import com.seeyii.web.download.mapper.RelatedMapper; import net.sf.json.JSONObject; public class QueryThread implements Callable<List<JSONObject>> { private RelatedMapper relatedMapper; private List<JSONObject> info_list; private int layerNum; /** * @param relatedMapper * @param info_list * @param layerNum */ public QueryThread(RelatedMapper relatedMapper, List<JSONObject> info_list, int layerNum) { this.relatedMapper = relatedMapper; this.info_list = info_list; this.layerNum = layerNum; } @Override public List<JSONObject> call() throws Exception { try { List<JSONObject> list = null; list = relatedMapper.queryShareholders(info_list,layerNum); return list; } catch (Exception e) { e.printStackTrace(); return null; } } }