java利用Future实现多线程执行与结果聚合实例代码
目录
- 场景
- 解决
- 总结
场景 【java利用Future实现多线程执行与结果聚合实例代码】网站智能问答场景,需要对多个分类查询,结果聚合展示
由于每种分类都有自己的业务逻辑,有的需要查询数据库中间库,有的需要查询elasticsearch搜索引擎,有的需要调用第三方接口,数据查询要分开进行,没法一次查询搞定
实际上这几个查询不相关,可以同时进行,现在串行,使该场景下,智能问答返回较慢
解决 最简单的逻辑,肯定就是java多线程,将串行改为并行
这样查询返回时间,就取决于最慢的一个查询,返回时间大大缩短
页面返回一般要求三秒内,实际项目上我们要求1秒内返回,多线程解决了这个问题
下面上代码,部分截取
@Autowiredprivate ThreadPoolTaskExecutor taskExecutor;
// 新闻查询SolrPageQueryVO newsQueryVO = new SolrPageQueryVO(); BeanUtil.copyProperties(vo, newsQueryVO); newsQueryVO.setAllSite(vo.getAllSite()); newsQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.articleNews.toString().concat(",").concat(SolrPageQueryVO.TypeCode.pictureNews.toString()).concat(",").concat(SolrPageQueryVO.TypeCode.videoNews.toString())); Future> newsFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, newsQueryVO, "news", context)); //网上服务Future> workGuideFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, vo, "workGuide", context)); //留言SolrPageQueryVO messageBoardQueryVO = new SolrPageQueryVO(); BeanUtil.copyProperties(vo, messageBoardQueryVO); messageBoardQueryVO.setAllSite(vo.getAllSite()); messageBoardQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.messageBoard.toString()); Future> messageBoardFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, messageBoardQueryVO, "messageBoard", context)); //信息公开(isAllSite为true时,搜索所有集合,不区分集合和站点,只根据dn搜索,有区分需要的项目可以重写SearchEsServiceImpl类)SolrPageQueryVO publicContentQueryVO = new SolrPageQueryVO(); BeanUtil.copyProperties(vo, publicContentQueryVO); publicContentQueryVO.setAllSite(vo.getAllSite()); publicContentQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.public_content.toString()); Future> publicContentFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, publicContentQueryVO, "public_content", context)); //问答知识库(isAllSite为true时,搜索所有集合,不区分集合和站点,有区分需要的项目可以重写或传false)SolrPageQueryVO knowledgeBaseQueryVO = new SolrPageQueryVO(); BeanUtil.copyProperties(vo, knowledgeBaseQueryVO); knowledgeBaseQueryVO.setAllSite(vo.getAllSite()); knowledgeBaseQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.knowledgeBase.toString()); Future> knowledgeBaseFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, knowledgeBaseQueryVO, "knowledgeBase", context)); try {knowledgeBaseFuture.get(); } catch (Exception e) {e.printStackTrace(); }try {messageBoardFuture.get(); } catch (Exception e) {e.printStackTrace(); }try {newsFuture.get(); } catch (Exception e) {e.printStackTrace(); }try {publicContentFuture.get(); } catch (Exception e) {e.printStackTrace(); }try {workGuideFuture.get(); } catch (Exception e) {e.printStackTrace(); }tabcount = sumMap.values().size(); map.put("tabcount", tabcount); map.put("numMap", sumMap);
private void selectForAsk(Map map, Map sumMap, SolrPageQueryVO vo, String type, Context context) {if ("news".equals(type)) {try {// do something} catch (Exception e) {e.printStackTrace(); }} else if ("workGuide".equals(type)) {try {//网上办事查询调用接口// do something} catch (Exception e) {e.printStackTrace(); }} else if ("messageBoard".equals(type)) {try {// do something} catch (Exception e) {e.printStackTrace(); }} else if ("public_content".equals(type)) {try {Long queryCount = SearchQueryHolder.queryCount(vo); // do something} catch (Exception e) {e.printStackTrace(); }} else if ("knowledgeBase".equals(type)) {try {// do something} catch (Exception e) {e.printStackTrace(); }}}
总结 到此这篇关于java利用Future实现多线程执行与结果聚合的文章就介绍到这了,更多相关java Future实现多线程执行内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- Java基础-高级特性-枚举实现状态机