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

02-07 01:12