java利用Future实现多线程执行与结果聚合实例代码
程序员文章站
2022-03-08 19:41:10
目录场景解决总结场景网站智能问答场景,需要对多个分类查询,结果聚合展示由于每种分类都有自己的业务逻辑,有的需要查询数据库中间库,有的需要查询elasticsearch搜索引擎,有的需要调用第三方接口,...
场景
网站智能问答场景,需要对多个分类查询,结果聚合展示
由于每种分类都有自己的业务逻辑,有的需要查询数据库中间库,有的需要查询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(); } } }
总结
到此这篇关于java利用future实现多线程执行与结果聚合的文章就介绍到这了,更多相关java future实现多线程执行内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!