服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - Java多线程执行处理业务时间太久解决方法代码示例

Java多线程执行处理业务时间太久解决方法代码示例

2021-02-28 11:08sunlei198906 JAVA教程

这篇文章主要介绍了Java多线程执行处理业务时间太久解决方法代码示例的相关资料,具有一定借鉴价值,需要的朋友可以参考下。

背景:在政府开发了一个应用系统,主要功能是让企业填写企业资质信息,然后通过给定的公式,统计这一系列的信息,以得分的形式展示给政府领导查看。目前有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++) {
    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、线程的代码

?
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

延伸 · 阅读

精彩推荐
  • JAVA教程Java 散列存储详解及简单示例

    Java 散列存储详解及简单示例

    这篇文章主要介绍了Java 散列存储详解及简单示例的相关资料,需要的朋友可以参考下...

    java教程网4602020-08-02
  • JAVA教程springboot整合freemarker详解

    springboot整合freemarker详解

    本篇文章主要介绍了springboot整合freemarker详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    张楠楠4632020-10-05
  • JAVA教程MyBatis高级映射和查询缓存

    MyBatis高级映射和查询缓存

    这篇文章主要介绍了MyBatis高级映射和查询缓存的相关资料,需要的朋友可以参考下 ...

    鹿天斐1942020-05-23
  • JAVA教程Spring Security实现短信验证码登陆

    Spring Security实现短信验证码登陆

    这篇文章主要介绍了Spring Security实现短信验证码登陆,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友...

    雨点的名字2772020-06-28
  • JAVA教程Java中channel用法总结

    Java中channel用法总结

    这篇文章主要介绍了Java中channel用法,较为详细的总结了channel的定义、类型及使用技巧,需要的朋友可以参考下 ...

    liuzx323032019-12-20
  • JAVA教程运行jar程序时添加vm参数的方法

    运行jar程序时添加vm参数的方法

    下面小编就为大家带来一篇运行jar程序时添加vm参数的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Java教程网4782020-08-18
  • JAVA教程浅谈Java的两种多线程实现方式

    浅谈Java的两种多线程实现方式

    本篇文章主要介绍了浅谈Java的两种多线程实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    翡翠森林Z5042020-12-21
  • JAVA教程Java多线程并发编程(互斥锁Reentrant Lock)

    Java多线程并发编程(互斥锁Reentrant Lock)

    这篇文章主要介绍了ReentrantLock 互斥锁,在同一时间只能被一个线程所占有,在被持有后并未释放之前,其他线程若想获得该锁只能等待或放弃,需要的朋友...

    hackeris3662020-10-23