java利用Future实现多线程执行与结果聚合实例代码
作者:坚持是一种态度 时间:2023-09-24 11:07:18
场景
网站智能问答场景,需要对多个分类查询,结果聚合展示
由于每种分类都有自己的业务逻辑,有的需要查询数据库中间库,有的需要查询elasticsearch搜索引擎,有的需要调用第三方接口,数据查询要分开进行,没法一次查询搞定
实际上这几个查询不相关,可以同时进行,现在串行,使该场景下,智能问答返回较慢
解决
最简单的逻辑,肯定就是java多线程,将串行改为并行
这样查询返回时间,就取决于最慢的一个查询,返回时间大大缩短
页面返回一般要求三秒内,实际项目上我们要求1秒内返回,多线程解决了这个问题
下面上代码,部分截取
@Autowired
private 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<String, Object> map, Map<String, Object> 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();
}
}
}
来源:https://blog.csdn.net/u010882234/article/details/122127152
标签:java,future,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# .NET 中的缓存实现详情
2023-05-30 18:35:12
人工智能开发语言排行榜: 不死Java, 不朽C/C++, 新贵Python【推荐】
2023-03-12 16:06:54
![](https://img.aspxhome.com/file/2023/7/117537_0s.jpg)
使用反射方式获取JPA Entity的属性和值
2023-07-24 17:43:22
Android中让按钮拥有返回键功能的方法及重写返回键功能
2021-08-08 10:33:56
JAVA图片水印开发案例详解
2023-06-20 02:06:53
![](https://img.aspxhome.com/file/2023/9/129989_0s.jpg)
如何搭建新的WPF项目框架
2023-09-28 08:18:05
SpringBoot默认使用HikariDataSource数据源方式
2022-03-18 01:04:46
Java使用OCR技术识别验证码实现自动化登陆方法
2022-03-10 04:34:52
![](https://img.aspxhome.com/file/2023/9/105959_0s.png)
SpringBoot启动yaml报错的解决
2021-09-09 22:58:14
![](https://img.aspxhome.com/file/2023/9/69089_0s.png)
Android编辑框EditText与焦点变更监视器及文本变化监视器实现流程详解
2021-06-21 04:48:14
![](https://img.aspxhome.com/file/2023/6/98676_0s.png)
C#如何将DataTable导出到Excel解决方案
2022-03-30 18:44:28
C#中IEnumerable、ICollection、IList、List之间的区别
2022-07-27 18:15:07
J2EE Servlet上传文件到服务器并相应显示功能的实现代码
2023-12-05 16:36:14
![](https://img.aspxhome.com/file/2023/9/96049_0s.png)
Android实现页面滑动切换动画
2023-12-26 11:47:54
java字符串常用操作方法(查找、截取、分割)
2023-11-29 03:21:13
Java之常用类小结案例讲解
2022-10-08 07:34:24
![](https://img.aspxhome.com/file/2023/5/67995_0s.png)
Spring MVC 前端控制器 (DispatcherServlet)处理流程解析
2023-02-03 03:56:50
![](https://img.aspxhome.com/file/2023/7/128857_0s.png)
SpringBoot实战之高效使用枚举参数(原理篇)案例详解
2022-02-10 23:54:23
![](https://img.aspxhome.com/file/2023/5/77055_0s.jpg)
java比较器Comparable接口与Comaprator接口的深入分析
2022-01-20 11:51:09
C#基于Socket实现多人聊天功能
2021-12-06 12:00:47
![](https://img.aspxhome.com/file/2023/9/104359_0s.jpg)