Background: An application system has been developed in the government. The main function is to let the enterprise fill in the enterprise qualification information, and then use the given formula to count this series of information, and display it to the government leaders in the form of scores. Currently, 1,300 companies fill in the report. Since the score is displayed in real time, the statistics function is slow.
Code running process :
1. Find out 1300 corporate information
2. Traverse 1300 enterprise information and calculate the score information of each enterprise. Each company is expected to have a time of 0.3 seconds. Total 390 seconds. Causes page request timeout
3. Export (using jxl jar)
Solution:
Since it handles business, it is necessary to have threads that can return values. Use: Callable
Directly upload the code
1. The code that calls the thread
List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("rows");int taskSize = 20;// Create a thread pool ExecutorService pool = Executors.newFixedThreadPool(taskSize);// Create multiple tasks with return values List<Future> listFuture = new ArrayList<Future>();for (int i = 0; i < taskSize; i++) {System.out.println("I enable multithreading lalala");int evgCount = list.size()/taskSize;Callable c = new MyCallable(list.subList(evgCount*i, evgCount*(i+1)),session,staticFlag,declareService,declareMasterService,enterpriseQueryService);// Execute the task and get the Future object Future f = pool.submit(c);listFuture.add(f);}pool.shutdown();// Get the running results of all concurrent tasks List<Map<String, Object>> listResult = new ArrayList<Map<String, Object>>();for (Future f : listFuture) {List<Map<String, Object>> listModel = new ArrayList<Map<String, Object>>();try {listModel = (List<Map<String, Object>>) f.get();}catch (InterruptedException e) {e.printStackTrace();}catch (ExecutionException e) {e.printStackTrace();}listResult.addAll(listModel);}map.put("rows", listResult);2. Thread code
package usi.jszx.controller;import java.util.List;import java.util.Map;import java.util.concurrent.Callable;import javax.servlet.http.HttpSession;import org.apache.commons.lang3.StringUtils;import usi.jszx.entity.ScoreMain;import usi.jszx.service.DeclareMasterService;import usi.jszx.service.DeclareService;import usi.jszx.service.DeclareService;import usi.jszx.service.EnterpriseQueryService;import usi.sys.dto.AuthInfo;import usi.sys.util.ConstantUtil;class MyCallable implements Callable<Object> {//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- MyCallable(List<Map<String,Object>> list,HttpSession session,String staticFlag,DeclareService declareService,DeclareMasterService declareMasterService,EnterpriseQueryService enterpriseQueryService) {this.list = list;this.session = session;this.staticFlag = staticFlag;this.declareService = declareService;this.declareMasterService = declareMasterService;this.enterpriseQueryService = enterpriseQueryService;}@Overridepublic Object call() throws Exception {AuthInfo info = (AuthInfo)session.getAttribute(ConstantUtil.AUTH_INFO);for (int i = 0; i < list.size(); i++) {Map<String,Object> maplist = list.get(i);String mainId= maplist.get("ID")+"";this.gradeMaster(session, mainId, maplist.get("orgId")+"",declareMasterService,enterpriseQueryService);List<Map<String,Object>> listscore = declareMasterService.queryScoreMain(maplist.get("ID")+"",info.getRightType(), "report");// declareMasterService.queryScoreMain(mainId,info.getRightType(),isreport);int isdouble = 1;if(listscore.size()>30){maplist.put("SOCRETOTAL", listscore.get(46).get("SCORE"));isdouble = 2;} else if(listscore.size()>22){maplist.put("SOCRETOTAL", listscore.get(23).get("SCORE"));}if("3".equals(staticFlag)){for (int j = 0; j < 23; j++) {if(j<9){maplist.put("VALUE0"+(j+1), listscore.get(j*isdouble).get("SHOW_VALUE"));} else{maplist.put("VALUE"+(j+1), listscore.get(j*isdouble).get("SHOW_VALUE"));}}}//City display String COUNTYID = maplist.get("COUNTYID")+""; if("340826".equals(COUNTYID)||"341822".equals(COUNTYID)){maplist.put("CITYNAME",maplist.get("COUNTYNAME")+"");}//Corporate Type String DECLARE_EVALUATE = maplist.get("DECLARE_EVALUATE")+"";if("1".equals(DECLARE_EVALUATE)){maplist.put("DECLARE_EVALUATE_NAME","Declaration");} else{maplist.put("DECLARE_EVALUATE_NAME","Evaluation");}//Audit Status String SHSTATUS = maplist.get("SHSTATUS")+"";if("9".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Draft");} else if("0".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Enterprise Submission");} else if("1".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "City Review Passed");} else if("2".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "City Review Passed");} else if("3".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Provincial Review Passed");} else if("4".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Provincial review failed");} else if("5".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Provincial review");} else if("6".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Return to enterprise modification");} else if("7".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "City review");} else if("11".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Modify to declaration");} else if("12".equals(SHSTATUS)){maplist.put("STRSHSTATUS", "Modify to evaluation");}if("1".equals(staticFlag)){//Add a modification opinion List<Map<String, Object>> listDetail = declareService.queryAuditLog(mainId); if(listDetail.size()>0){String AUDIT_OPINION = listDetail.get(0).get("AUDIT_OPINION")+"";if(!StringUtils.isEmpty(AUDIT_OPINION)&&!"null".equals(AUDIT_OPINION)){maplist.put("AUDIT_OPINION", AUDIT_OPINION);} else{maplist.put("AUDIT_OPINION", "");}}//Is it used to be changed to String ORGNAME = maplist.get("ORGNAME")+"";String PJNAME = maplist.get("PJNAME")+"";if(StringUtils.isEmpty(PJNAME)||"null".equals(PJNAME)||PJNAME.equals(ORGNAME)){maplist.put("ISGENGMING", "No");maplist.put("PJNAME_E", "");} else{maplist.put("ISGENGMING", "Yes");maplist.put("PJNAME_E", PJNAME);} else if("2".equals(staticFlag)){}} return list;}public float gradeMaster(HttpSession session,String mainId,String orgId,DeclareMasterService declareMasterService,EnterpriseQueryService enterpriseQueryService) {AuthInfo info = (AuthInfo)session.getAttribute(ConstantUtil.AUTH_INFO);String rightType=info.getRightType();declareMasterService.deleteScoreMain(mainId);float[] resultFirst = new float[100];/** First query all the list of appendix* The place to view the score is to directly find the main table data* * Since the review has been made, the main table data must be stored* */List<Map<String,Object>> listDetail = declareMasterService.queryTaskDetail(mainId);if("2".equals(rightType)||"3".equals(rightType)){//Convert String to floatfor (int i = 0; i < listDetail.size(); i++) {Map<String,Object> map = listDetail.get(i);if(StringUtils.isEmpty(map.get("DECLARE_CITY_VALUE")+"")||"null".equals(map.get("DECLARE_CITY_VALUE")+"")){resultFirst[i]=0f;} else{resultFirst[i] = float.parsefloat(map.get("DECLARE_CITY_VALUE")+"");}}} else{//Convert String to floatfor (int i = 0; i < listDetail.size(); i++) {Map<String,Object> map = listDetail.get(i);if(StringUtils.isEmpty(map.get("DECLARE_PROVINCE_VALUE")+"")||"null".equals(map.get("DECLARE_PROVINCE_VALUE")+"")){resultFirst[i]=0f;} else{resultFirst[i] = float.parsefloat(map.get("DECLARE_PROVINCE_VALUE")+"");}}}Map<String,Object> enterprise= enterpriseQueryService.getInfoByOrgId(orgId).get(0);// Calculate the value according to the calculation formula of the evaluation index of Anhui Enterprise Technology Center. The next step is to calculate the score float ratio1 = 0f;float ratio2 = 0f;float ratio3 = 0f;try {ratio1 = float.parsefloat(enterprise.get("RATIO1")+"");ratio2 = float.parsefloat(enterprise.get("RATIO2")+"");ratio3 = float.parsefloat(enterprise.get("RATIO3")+"");}catch (Exception e) {}Map<String,Object> map = DeclareController.getValue(resultFirst,ratio1,ratio2,ratio3);float[] resultValue = (float[]) map.get("resultValue");float[] resultScoreValue = (float[]) map.get("resultScoreValue");float[] resultScore = DeclareController.getScore(resultScoreValue);float scoreTotal = 0f;List<Map<String,Object>> listScore = declareMasterService.queryScoreDic();for (int i = 0; i < listScore.size(); i++) {ScoreMain scoreMain = new ScoreMain(); scoreMain.setMainId(mainId); scoreMain.setScoreName(listScore.get(i).get("SCORE_NAME")+""); scoreMain.setScoreUnit(listScore.get(i).get("SCORE_UNIT")+""); scoreMain.setScoreWeight(listScore.get(i).get("SCORE_WEIGHT")+""); scoreMain.setScoreWeight(listScore.get(i).get("SCORE_WEIGHT")+""); scoreMain in.setDisOrder(listScore.get(i).get("DIS_ORDER")+""); scoreMain.setShowValue(resultValue[i]+""); scoreMain.setScoreValue(resultScoreValue[i]+""); scoreMain.setScore(resultScore[i]+""); declareMasterService.inserScoreMain(scoreMain); scoreTotal +=resultScore[i];}return scoreTotal;}}Note: MyCallable is just a complicated way of handling business. It can be ignored, and finally speed up from 390 seconds to 40 seconds.
Summarize
The above is the entire content of this article about the solution code example of Java multi-threaded execution and processing business time too long. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!