欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合實(shí)例代碼

 更新時(shí)間:2021年12月26日 08:51:12   作者:堅(jiān)持是一種態(tài)度  
這篇文章主要給大家介紹了關(guān)于java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合的相關(guān)資料,Future模式的核心,去除了主函數(shù)的等待時(shí)間,并使得原本需要等待的時(shí)間段可以用于處理其他業(yè)務(wù)邏輯,需要的朋友可以參考下

場(chǎng)景

網(wǎng)站智能問(wèn)答場(chǎng)景,需要對(duì)多個(gè)分類(lèi)查詢,結(jié)果聚合展示

由于每種分類(lèi)都有自己的業(yè)務(wù)邏輯,有的需要查詢數(shù)據(jù)庫(kù)中間庫(kù),有的需要查詢elasticsearch搜索引擎,有的需要調(diào)用第三方接口,數(shù)據(jù)查詢要分開(kāi)進(jìn)行,沒(méi)法一次查詢搞定

實(shí)際上這幾個(gè)查詢不相關(guān),可以同時(shí)進(jìn)行,現(xiàn)在串行,使該場(chǎng)景下,智能問(wèn)答返回較慢

解決

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

這樣查詢返回時(shí)間,就取決于最慢的一個(gè)查詢,返回時(shí)間大大縮短

頁(yè)面返回一般要求三秒內(nèi),實(shí)際項(xiàng)目上我們要求1秒內(nèi)返回,多線程解決了這個(gè)問(wèn)題

下面上代碼,部分截取

	@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));

            //信息公開(kāi)(isAllSite為true時(shí),搜索所有集合,不區(qū)分集合和站點(diǎn),只根據(jù)dn搜索,有區(qū)分需要的項(xiàng)目可以重寫(xiě)SearchEsServiceImpl類(lèi))
            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));


            //問(wèn)答知識(shí)庫(kù)(isAllSite為true時(shí),搜索所有集合,不區(qū)分集合和站點(diǎn),有區(qū)分需要的項(xiàng)目可以重寫(xiě)或傳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();
            }
        }
    }

總結(jié)

到此這篇關(guān)于java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合的文章就介紹到這了,更多相關(guān)java?Future實(shí)現(xiàn)多線程執(zhí)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中Lombok常用注解分享

    Java中Lombok常用注解分享

    以前的Java項(xiàng)目中充斥了太多不友好的代碼,這些代碼不僅沒(méi)有什么技術(shù)含量,還影響代碼的美觀,所以Lombok應(yīng)運(yùn)而生了。本文和大家分享了一些Java中Lombok常用注解,需要的可以了解一下
    2023-04-04
  • SpringBoot中jar啟動(dòng)下如何讀取文件路徑

    SpringBoot中jar啟動(dòng)下如何讀取文件路徑

    這篇文章主要介紹了SpringBoot?jar啟動(dòng)下如何讀取文件路徑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • java URL 獲取PHP JSON 數(shù)據(jù)

    java URL 獲取PHP JSON 數(shù)據(jù)

    這篇文章主要介紹了java URL 獲取PHP JSON 數(shù)據(jù),需要的朋友可以參考下
    2016-04-04
  • java面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)與依賴倒置原則詳解

    java面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)與依賴倒置原則詳解

    這篇文章主要介紹了java面向?qū)ο笤O(shè)計(jì)原則之單一職責(zé)與依賴倒置原則的分析詳解,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-10-10
  • springboot使用RedisRepository操作數(shù)據(jù)的實(shí)現(xiàn)

    springboot使用RedisRepository操作數(shù)據(jù)的實(shí)現(xiàn)

    本文主要介紹了springboot使用RedisRepository操作數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 淺談Java Fork/Join并行框架

    淺談Java Fork/Join并行框架

    這篇文章主要介紹了淺談Java Fork/Join并行框架,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • java.lang.StackOverflowError出現(xiàn)的原因及解決

    java.lang.StackOverflowError出現(xiàn)的原因及解決

    這篇文章主要介紹了java.lang.StackOverflowError出現(xiàn)的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java中的clone方法實(shí)例詳解

    Java中的clone方法實(shí)例詳解

    這篇文章主要介紹了Java中的clone方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 實(shí)例詳解java Struts2的配置與簡(jiǎn)單案例

    實(shí)例詳解java Struts2的配置與簡(jiǎn)單案例

    這篇文章主要介紹了java Struts2的配置與簡(jiǎn)單案例,需要的朋友可以參考下
    2017-04-04
  • 解析java中This的用法分析

    解析java中This的用法分析

    本篇文章是對(duì)java中This的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論