多线程查数据库实例
2022/8/6 2:22:43
本文主要是介绍多线程查数据库实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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; } } }
这篇关于多线程查数据库实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?