背景:在政府開發了一個應用系統,主要功能是讓企業填寫企業資質信息,然后通過給定的公式,統計這一系列的信息,以得分的形式展示給政府領導查看。目前有1300家企業填報。由于得分是實時顯示的,所以導致統計功能很慢。
代碼運行流程:
1、查出1300企業信息
2、遍歷1300企業信息,ji計算每家企業得分信息。每家預計時間為0.3秒。合計390秒。導致頁面請求超時
3、導出(用jxl jar)
解決方案:
由于處理業務的,所以需要能有返回值的線程。用:Callable
直接上代碼
1、調用線程的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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++) { 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、線程的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
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多線程執行處理業務時間太久解決方法代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://www.zuidaima.com/blog/3662802104159232.htm