您好,登錄后才能下訂單哦!
java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合的代碼怎么寫,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
網(wǎng)站智能問答場景,需要對多個分類查詢,結(jié)果聚合展示
由于每種分類都有自己的業(yè)務(wù)邏輯,有的需要查詢數(shù)據(jù)庫中間庫,有的需要查詢elasticsearch搜索引擎,有的需要調(diào)用第三方接口,數(shù)據(jù)查詢要分開進(jìn)行,沒法一次查詢搞定
實(shí)際上這幾個查詢不相關(guān),可以同時(shí)進(jìn)行,現(xiàn)在串行,使該場景下,智能問答返回較慢
最簡單的邏輯,肯定就是java多線程,將串行改為并行
這樣查詢返回時(shí)間,就取決于最慢的一個查詢,返回時(shí)間大大縮短
頁面返回一般要求三秒內(nèi),實(shí)際項(xiàng)目上我們要求1秒內(nèi)返回,多線程解決了這個問題
下面上代碼,部分截取
@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)); //網(wǎng)上服務(wù) 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時(shí),搜索所有集合,不區(qū)分集合和站點(diǎn),只根據(jù)dn搜索,有區(qū)分需要的項(xiàng)目可以重寫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時(shí),搜索所有集合,不區(qū)分集合和站點(diǎn),有區(qū)分需要的項(xiàng)目可以重寫或傳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 { //網(wǎng)上辦事查詢調(diào)用接口 // 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(); } } }
關(guān)于java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合的代碼怎么寫問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。