Java多线程执行处理业务时间太久解决方法代码示例
程序员文章站
2023-12-04 11:24:10
背景:在*开发了一个应用系统,主要功能是让企业填写企业资质信息,然后通过给定的公式,统计这一系列的信息,以得分的形式展示给*领导查看。目前有1300家企业填报。由于得分...
背景:在*开发了一个应用系统,主要功能是让企业填写企业资质信息,然后通过给定的公式,统计这一系列的信息,以得分的形式展示给*领导查看。目前有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多线程执行处理业务时间太久解决方法代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!