java多次嵌套循環(huán)查詢數(shù)據(jù)庫導(dǎo)致代碼中數(shù)據(jù)處理慢的解決
業(yè)務(wù)現(xiàn)象
代碼中有一部分代碼多次嵌套循環(huán)和數(shù)據(jù)處理,執(zhí)行速度很慢
解決方案
通過多線程
1、啟用多線程
private final static Executor executor = Executors.newFixedThreadPool(3);
2、初始化設(shè)置count
即等待(await)count個(gè)線程或一個(gè)線程count次計(jì)數(shù),通過工作線程來countDown計(jì)數(shù)減一,直到計(jì)數(shù)為0,await阻塞結(jié)束;目的:保證所有線程都走完
final CountDownLatch latch = new CountDownLatch(dataList.size());
3、需要重新run
需要多線程的代碼寫在run中
?@Override
? ? ? ?public void run() {?
? ? ? ? ? ? ? ? //業(yè)務(wù)代碼處理
? ? ? ? ? ? ? ? //countDown計(jì)數(shù)減一
? ? ? ? ? ? ? ? ?latch.countDown();
}4、阻塞線程
? // 等待所有工作線程結(jié)束 ? ? ? ? ? ? latch.await();
關(guān)鍵代碼
private final static Executor executor = Executors.newFixedThreadPool(3);//啟用多線程
public Result getList(@RequestBody StatusDbSelectParam param){
PageHelper.startPage(param.getPageNum(), param.getPageSize());
List<Map<String, Object>> dataList = statusDbService.selectByTime(tableName, columnNames.toString(), param.getStartTime(), param.getEndTime());
//初始化設(shè)置count,即等待(await)count個(gè)線程或一個(gè)線程count次計(jì)數(shù),通過工作線程來countDown計(jì)數(shù)減一,直到計(jì)數(shù)為0,await阻塞結(jié)束
final CountDownLatch latch = new CountDownLatch(dataList.size());
for(int k =0;k<dataList.size();k++) {
final int i = k;
String finalTableName = tableName;
executor.execute(new Runnable() {
@Override
public void run() {
try {
Map<String, Object> map = dataList.get(i);
for (String pid : map.keySet()) {
String val = String.valueOf(map.get(pid));
String columPid = pid.substring(2, pid.length()).toLowerCase();
List<MonitorRuleEntity> list = monitorRuleService.listMonitorRule(columPid, finalTableName);
String status = "";
//循環(huán)規(guī)則數(shù)據(jù) 判斷監(jiān)測(cè)點(diǎn)是否報(bào)警 更新狀態(tài)
for (int j = 0; j < list.size(); j++) {
Boolean flag = DevHealthStatusFactory.getInstance().getResultMapByRule(val, list.get(j));
if (flag) {
if (j == 0) {
status = "A";
break;
} else if (j == 1) {
status = "B";
break;
} else if (j == 2) {
status = "C";
break;
}
}
}
if (pid.equalsIgnoreCase(timeColumnName)) {
map.put(pid, val);
} else {
map.put(pid, val + "_" + status);
}
}
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
try {
// 等待所有工作線程結(jié)束
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return ResultMsg.successMsg().data(new PageInfo<>(dataList));
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java密鑰交換算法DH定義與應(yīng)用實(shí)例分析
這篇文章主要介紹了java密鑰交換算法DH定義與應(yīng)用,結(jié)合實(shí)例形式分析了Java密鑰交換算法DH的原理、定義、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
JavaCV實(shí)現(xiàn)圖片中人臉檢測(cè)的示例代碼
這篇文章主要介紹了如何利用JavaCV實(shí)現(xiàn)圖片中人臉檢測(cè)的功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-11-11
ShardingSphere jdbc實(shí)現(xiàn)分庫分表核心概念詳解
這篇文章主要為大家介紹了ShardingSphere jdbc實(shí)現(xiàn)分庫分表核心概念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))的相關(guān)資料,在開發(fā)項(xiàng)目漫長(zhǎng)的過程中常常會(huì)遇到流水號(hào)需要自動(dòng)生成的問題存在,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07
Spring boot集成redis lettuce代碼實(shí)例
這篇文章主要介紹了Spring boot集成redis lettuce代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
關(guān)于Sentinel中冷啟動(dòng)限流原理WarmUpController
這篇文章主要介紹了關(guān)于Sentinel中冷啟動(dòng)限流原理WarmUpController,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-04-04
java模擬http請(qǐng)求的錯(cuò)誤問題整理
本文是小編給大家整理的在用java模擬http請(qǐng)求的時(shí)候遇到的錯(cuò)誤問題整理,以及相關(guān)分析,有興趣的朋友參考下。2018-05-05

