多线程查询

/**
* 多线程查询
* @param fileDate
* @return
* @throws Exception
*/
public List<Object[]> queryList(String fileDate) throws Exception{
long start = System.currentTimeMillis();
List<Object[]> result=new ArrayList<>();

Integer count = shareDao.findCount(fileDate);
logger.info("总共数量:{}"+count);
//需要查询的次数
int times=count / num;
if(count%num !=0) {
times=times+1;
}
//开始查询的行数
int bindex = 0;
List<Callable<List<Object[]>>> tasks = new ArrayList<Callable<List<Object[]>>>();//添加任务
for(int i = 0; i <times ; i++){
Callable<List<Object[]>> qfe = new ThredQuery(shareDao,fileDate,bindex, num);
tasks.add(qfe);
bindex=bindex+num;
}
logger.info("总共多少页:{}"+times);
ExecutorService execservice = Executors.newFixedThreadPool(threadcount);

List<Future<List<Object[]>>> futures = execservice.invokeAll(tasks);
// 处理线程返回结果
if (futures != null && futures.size() > 0) {
for(Future<List<Object[]>> future : futures) {
result.addAll(future.get());
}
}
execservice.shutdown();
long end = System.currentTimeMillis();
logger.info("查询总数量:{"+count+"},执行结果数量:{"+result.size()+"},总用时:{"+(end-start)+"}");
return result;
}
posted @ 2022-06-30 16:02  Struts-pring  阅读(189)  评论(0编辑  收藏  举报