배경 : 정부에서 신청 시스템이 개발되었습니다. 주요 기능은 기업이 기업 자격 정보를 채우고 주어진 공식을 사용 하여이 일련의 정보를 계산하여 점수 형태로 정부 리더에게 표시하는 것입니다. 현재 1,300 개의 회사가 보고서를 작성합니다. 점수가 실시간으로 표시되므로 통계 기능은 느립니다.
코드 실행 프로세스 :
1. 1300 개의 회사 정보를 찾으십시오
2. 트래버스 1300 엔터프라이즈 정보 및 각 기업의 점수 정보를 계산하십시오. 각 회사의 시간은 0.3 초가 될 것으로 예상됩니다. 총 390 초. 페이지 요청 시간 초과 원인
3. 내보내기 (JXL JAR 사용)
해결책:
비즈니스를 처리하므로 값을 반환 할 수있는 스레드가 필요합니다. 사용 : 호출 가능
코드를 직접 업로드하십시오
1. 스레드를 호출하는 코드
List <map <string, object >> list = (list <map <string, object >>) map.get ( "rows"); int tasksize = 20; int tasksize = 20; // executorService pool = executors.newFixedThreadPool (taskSize); // 리턴 값 목록 <cuture> listFuture = new arrayList> (); INT I = 0; i <0). {System.out.println ( "I anable multithreading lalala"); int evgcount = list.size ()/tasksize; CAMEBLE C = new MyCallable (ist.sublist (evgCount*i, evgcount*(i+1)), 세션, staticflag, 선언, 선언적 보존, Enterprise -service, Enterprise -service thepriseverevice); 미래의 객체 F = pool.submit (c); listfuture.add (f);} pool.shutdown (); // 모든 동시 작업 목록 <map <문자열, 객체 >> listresult = new arraylist <string, object (); for (future <map <string, object >> listmomodel = new araterylist >> 객체의 실행 결과 가져옵니다. Object >> (); try {listModel = (list <map <string, object >>) f.get ();} catch (InterpruptedException e) {e.printstacktrace () catch (executionException e) {e.printstacktrace ();} listresult.addall (listmodel);} prut ( "rows";2. 스레드 코드
패키지 usi.jszx.controller; import java.util.list; import java.util.map; import java.util.callable.callable; import javax.servlet.http.httpsession; import org.apache.commons.lang3.stringutils; import usi.jszx.entity 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 empless callable <botort> {//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- myCallable (list <map <string, object >> 목록, httpsession 세션, 문자열 staticflag, deplareservice declareservice, declaremasterService deLlareMasterService, EnterpriseRegerVice EnterpriseQueryService) {this.list = list; this.session = session; this.staticflag = staticflag; deMareService; this.declaremasterService = declaremasterService; this. enderPriseQueryService = EnterpriseQueryService;}@atriadePublic Object () {authinfo info = (authinfo) session.getAttribute (constantutil.auth_info); in <list (int i =); {map <string, object> maplist = list.get (i); String mainid = maplist.get ( "id")+""; this.grademaster (세션, mainid, mainid, maplist.get ( "orgid")+"", DeclaremasterService, EnterpriseQueryService); list <map <string, object >> listscore = deMaremasterService.queryScoremain (maplist.get ( "id")+"", info.getrighttype (), "report"); // deMaremasterService.queryScoremain (mainId, info.getrighttype (), isreport = 1; if (listscore.size ()> 30) {maplist. " ListScore.get (46) .get ( "score")); isdouble = 2;} else if (listscore.size ()> 22) {maplist.put ( "socretotal", listscore.get (23) .get ( "score"));} if ( "3".equals (staticflag)) {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);}}}}}}}}}}}}}} maplist.get ( "CountyID")+""; if ( "340826".Equals (CountyID) || "341822".Equals (CountyId)) {maplist.put ( "CityName", maplist.get ( "CountyName")+"");} // Corporate String declare_evaluate = maplist.get ( "deplare_evaluate")+"" "; if ("1 ".equals (delectr (deleclare_evaluate))) {maplist.put ("deplare_evaluate_name ","deplaration ");} else {maplist.put ("deplare_evaluate_name ","Evaluation ");} // audit shstatus = maplist.get ( "shstatus")+""; if ( "9".equals (shstatus))) {maplist.put ( "strshstatus", "draft");} else if ( "0".equals (shstatus)) {maplist.put ( "strshstatus", "Enterprise Submission");} if ( "1".equals (shstatus)) {maplist.put ( "strshstatus", "city review passed");} else if ( "2".equals (shstatus)) {maplist.put ( "strshstatus", "City Review"); else if ( "3". "지방 검토 통과");} else if ( "4".equals (shstatus) (shstatus))) {maplist.put ( "strshstatus", "Provincial Review");} else if ( "5".equals (shstatus)) {maplist.put ( "strshstatus", "Provincial Review");} if ( "6".equals (shstatus)) {maplist.put ( "strshstatus", "Enterprise Modification으로 돌아 가기");} else if ( "7".equals (shstatus)) {maplist.put ( "strshstatus", "City Review"); else if ( "11". " "선언으로 수정");} else if ( "12".equals (shstatus) (shstatus))) {maplist.put ( "strshstatus", "Evaluation으로 수정");} if ( "1".Equals (staticFlag)) {// 수정 의견 목록 <map <string, object >> listDetail = declarEservice.queryOud. if (listDetail.size ()> 0) {String audit_opinion = listdetail.get (0) .get ( "audit_opinion")+""; if (! stringUtils.isempty (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", "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); restfirst = new float [/** 직접적으로 직접 촉발된다*. 테이블 데이터 * * 검토가 작성되었으므로 메인 테이블 데이터는 */list <map <string, object >> 객체 >> listDetail = listDetail = deLlaremasterService.queryTaskDetail (mainId); if ( "2".equals (RightType) || "3".equals (righttype)) {// string to floatfor (inst i = 0; i <listedeail (<sondetail). {map <string, object> map = listdetail.get (i); if (stringUtils.isempty ( "deplare_city_value")+")") || "null".equals ( "deMelare_city_value"))) {resultFirst [i] = 0f;} else} float.parsefloat (map.get ( "deplare_city_value")+";}}} else {// string을 floatfor로 변환합니다 (int i = 0; i <listdetail.size (); i ++) {map <string, object> map = listDetail.get (i); if (stringUtils.isempty (map.get ( "deplare_province_value")+"") || "null".equals (map.get ( "deplare_province_value")+"))) {resultfirst [i] = 0f;} else {resultfirst [i] = float.parsefloat (map.get ( "deplare_province_value")+");}}} map <문자열, 개체> Enterprise = EnterpriseQueryService.getInfobyorgid (orgid). get (0); Anhui Enterprise 기술 센터의 평가 지수의 계산 공식에 따라 값을 계산합니다. 다음 단계는 스코어 플로트 비율 1 = 0f; float ratio2 = 0f; float ratio3 = 0f; try {ratio1 = float.parsefloat (Enterprise.get ( "ratio1")+")"; ratio2 = float.parsefloat (Enterprise.get ( "ratio2")+""); float.parsefloat (Enterprise.get ( "ratio3")+"");} catch (Exception e) {} map <string, object> map = deLlareController.getValue (resultFirst, ratio1, rat ratio3, rat ratio3); float [] resultValue = (float []) map.Get ( "resultValue"); map.get ( "resultScoreValue"); float [] resultScore = deLlareController.getScore (resultScoreValue); float scoreTotal = 0f; list <map <string, object >> listScore = listScore = deLlaremasterService.querycordic (); for (intscore.size (); scoremain.setmainid (mainid); scoremain.setscoreName (listScore.get (i) .get ( "stacy_name")+""); scoremain.setscoreUnit (listscore.get (i) .get ( "stacy_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]+""); deLaremasterService.inserscoremain (scoremain); skoretotal += resultscore [i];} return skoretotal;}}참고 : MyCallable은 사업을 처리하는 복잡한 방법 일뿐입니다. 무시할 수 있으며 마지막으로 390 초에서 40 초로 속도가 높아집니다.
요약
위는 Java 다중 스레드 실행 및 처리 비즈니스 시간의 솔루션 코드 예제에 대한이 기사의 전체 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!