Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code that java uses Future to implement multithread execution and result aggregation

Shulou Source: shulou.com Published: 2022-06-02 05:14:23 09月13日 Update

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.

Tags: Query thread question search results project question and answer code scenario reality interface data time intelligence more knowledge site logic classification help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Xiaomi Huawei macOS