日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

java利用Future实现多线程执行与结果聚合的代码怎么写

發布時間:2023/12/15 综合教程 23 生活家
生活随笔 收集整理的這篇文章主要介紹了 java利用Future实现多线程执行与结果聚合的代码怎么写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java利用Future實現多線程執行與結果聚合的代碼怎么寫,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

場景

網站智能問答場景,需要對多個分類查詢,結果聚合展示

由于每種分類都有自己的業務邏輯,有的需要查詢數據庫中間庫,有的需要查詢elasticsearch搜索引擎,有的需要調用第三方接口,數據查詢要分開進行,沒法一次查詢搞定

實際上這幾個查詢不相關,可以同時進行,現在串行,使該場景下,智能問答返回較慢

解決

最簡單的邏輯,肯定就是java多線程,將串行改為并行

這樣查詢返回時間,就取決于最慢的一個查詢,返回時間大大縮短

頁面返回一般要求三秒內,實際項目上我們要求1秒內返回,多線程解決了這個問題

下面上代碼,部分截取

	@Autowired
privateThreadPoolTaskExecutortaskExecutor;
//新聞查詢
SolrPageQueryVOnewsQueryVO=newSolrPageQueryVO();
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));

//留言
SolrPageQueryVOmessageBoardQueryVO=newSolrPageQueryVO();
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類)
SolrPageQueryVOpublicContentQueryVO=newSolrPageQueryVO();
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)
SolrPageQueryVOknowledgeBaseQueryVO=newSolrPageQueryVO();
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(Exceptione){
e.printStackTrace();
}
try{
messageBoardFuture.get();
}catch(Exceptione){
e.printStackTrace();
}
try{
newsFuture.get();
}catch(Exceptione){
e.printStackTrace();
}
try{
publicContentFuture.get();
}catch(Exceptione){
e.printStackTrace();
}
try{
workGuideFuture.get();
}catch(Exceptione){
e.printStackTrace();
}

tabcount=sumMap.values().size();
map.put("tabcount",tabcount);
map.put("numMap",sumMap);
privatevoidselectForAsk(Map<String,Object>map,Map<String,Object>sumMap,SolrPageQueryVOvo,Stringtype,Contextcontext){
if("news".equals(type)){
try{
//dosomething
}catch(Exceptione){
e.printStackTrace();
}
}elseif("workGuide".equals(type)){
try{
//網上辦事查詢調用接口
//dosomething
}catch(Exceptione){
e.printStackTrace();
}
}elseif("messageBoard".equals(type)){
try{
//dosomething
}catch(Exceptione){
e.printStackTrace();
}
}elseif("public_content".equals(type)){
try{
LongqueryCount=SearchQueryHolder.queryCount(vo);
//dosomething
}catch(Exceptione){
e.printStackTrace();
}
}elseif("knowledgeBase".equals(type)){
try{
//dosomething
}catch(Exceptione){
e.printStackTrace();
}
}
}

總結

以上是生活随笔為你收集整理的java利用Future实现多线程执行与结果聚合的代码怎么写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。