您好,登錄后才能下訂單哦!
背景:在政府開發了一個應用系統,主要功能是讓企業填寫企業資質信息,然后通過給定的公式,統計這一系列的信息,以得分的形式展示給政府領導查看。目前有1300家企業填報。由于得分是實時顯示的,所以導致統計功能很慢。
代碼運行流程:
1、查出1300企業信息
2、遍歷1300企業信息,ji計算每家企業得分信息。每家預計時間為0.3秒。合計390秒。導致頁面請求超時
3、導出(用jxl jar)
解決方案:
由于處理業務的,所以需要能有返回值的線程。用:Callable
直接上代碼
1、調用線程的代碼
List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("rows"); int taskSize = 20; // 創建一個線程池 ExecutorService pool = Executors.newFixedThreadPool(taskSize); // 創建多個有返回值的任務 List<Future> listFuture = new ArrayList<Future>(); for (int i = 0; i < taskSize; i++) { System.out.println("我啟用多線程啦啦啦"); int evgCount = list.size()/taskSize; Callable c = new MyCallable(list.subList(evgCount*i, evgCount*(i+1)),session,staticFlag, declareService,declareMasterService,enterpriseQueryService); // 執行任務并獲取Future對象 Future f = pool.submit(c); listFuture.add(f); } pool.shutdown(); // 獲取所有并發任務的運行結果 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、線程的代碼
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.EnterpriseQueryService; import usi.sys.dto.AuthInfo; import usi.sys.util.ConstantUtil; class MyCallable implements Callable<Object> { //-----------------以下為線程調用的方法---------------- private List<Map<String,Object>> list; private HttpSession session; private String staticFlag; private DeclareService declareService; private DeclareMasterService declareMasterService; private EnterpriseQueryService enterpriseQueryService; public 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; } @Override public 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")); } } } //地市展示 String COUNTYID = maplist.get("COUNTYID")+""; if("340826".equals(COUNTYID)||"341822".equals(COUNTYID)){ maplist.put("CITYNAME",maplist.get("COUNTYNAME")+""); } //企業類型 String DECLARE_EVALUATE = maplist.get("DECLARE_EVALUATE")+""; if("1".equals(DECLARE_EVALUATE)){ maplist.put("DECLARE_EVALUATE_NAME","申報"); } else{ maplist.put("DECLARE_EVALUATE_NAME","評價"); } //審核狀態 String SHSTATUS = maplist.get("SHSTATUS")+""; if("9".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "草稿"); } else if("0".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "企業提交"); } else if("1".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "市審核通過"); } else if("2".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "市審核不通過"); } else if("3".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "省審核通過"); } else if("4".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "省審核不通過"); } else if("5".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "省級審核中"); } else if("6".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "退回企業修改"); } else if("7".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "市級審核中"); } else if("11".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "修改為申報"); } else if("12".equals(SHSTATUS)){ maplist.put("STRSHSTATUS", "修改為評價"); } if("1".equals(staticFlag)){ //添加修改意見 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", ""); } } //是否更名 曾用名 String ORGNAME = maplist.get("ORGNAME")+""; String PJNAME = maplist.get("PJNAME")+""; if(StringUtils.isEmpty(PJNAME)||"null".equals(PJNAME) ||PJNAME.equals(ORGNAME)){ maplist.put("ISGENGMING", "否"); maplist.put("PJNAME_E", ""); } else{ maplist.put("ISGENGMING", "是"); 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]; /* * 先查詢所有 附表列表 * 查看得分的地方,是直接查找主表數據的 * * 既然審核了,主表數據肯定存起來了 * */ List<Map<String,Object>> listDetail = declareMasterService.queryTaskDetail(mainId); if("2".equals(rightType)||"3".equals(rightType)){ //將String 轉為 float for (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{ //將String 轉為 float for (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); //根據 安徽省企業技術中心評價指標計算公式 進行算值 下一步算分 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.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; } }
說明:MyCallable僅僅是業務處理方式繁雜。可忽略,最后從390秒提速致40秒。
總結
以上就是本文關于Java多線程執行處理業務時間太久解決方法代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。