In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java uses Future to implement multithreaded execution and result aggregation. In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Scene
Intelligent Q & A scenario on the website requires multiple classified queries and aggregate display of the results
Since each category has its own business logic, some need to query the database intermediate database, some need to query the elasticsearch search engine, and some need to call third-party interfaces. Data query should be carried out separately, so it is impossible to query at one time.
In fact, these queries are not related and can be carried out at the same time, now serial, so that in this scenario, intelligent Q & A returns slowly.
Solve
The simplest logic must be java multithreading, changing serial to parallel.
In this way, the query return time depends on the slowest query, and the return time is greatly shortened.
It usually takes less than three seconds for the page to return. On the actual project, we need to return within one second. Multithreading solves this problem.
Code below, partial intercept
@ Autowired private ThreadPoolTaskExecutor taskExecutor;// News query 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)) / / online service Future workGuideFuture = taskExecutor.submit (()-> selectForAsk (map, sumMap, vo, "workGuide", context); / / leave a message 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)); / / Information disclosure (when isAllSite is true, search all collections, do not distinguish between collections and sites, only search based on dn, items with differentiated needs can be rewritten SearchEsServiceImpl classes) 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)) / / question and answer knowledge base (when isAllSite is true, search all collections, do not distinguish between collections and sites, items with differentiated needs can be rewritten or transferred to 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 map, Map 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 {/ / query API / / 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 () } this is the answer to the question about how to write the code for java to implement multithreaded execution and result aggregation using Future. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.