当前位置:网站首页>多线程使用redis进行累加结果不对解决方案
多线程使用redis进行累加结果不对解决方案
2022-04-22 16:30:00 【时间是一种解药】
多线程使用redis进行累加结果不对解决方案
问题背景
在使用多线程进行对redis的value做累加时导致出错,使用伪代码进行原因分析
for(int i=0;i<10;i++){
//提交10个线程,每个params装有10条数据
asyncServiceExecutor.submit(() -> submitSingle(globalId, params));
}
public Map<String,String> submitSingle(String globalId, JSONArray params) {
try {
log.info("params: {}, size: {}", params, params.size());
Map<String,String> resultVOlist = QueryController.doQuery(params);
//由于是多核,可以做到真正意义上同时间的并发,因此可能会出现有5个线程同时到达了这条语句
//第一次同时达到,redis里面就是没有这个globalId关键字,因此5个线程都是为null
Object commitObject = redisUtils.get(globalId);
//当有5个为null的线程进来之后,就会导致多次创建了同一个键,累加数据相当于丢失了4次,也就是40条
if (commitObject == null) {
boolean flag = redisUtils.set(globalId, params.size());
} else {
redisUtils.increment(globalId, (long) params.size());
}
return resultVOlist;
} catch (Exception e) {
log.error("QueryController not found error", e);
return new HashMap<>();
}
}
解决方案
1 既然是多线程产生的原因,那么我们可以把写redis的写操作移出到主线程,redis的底层写操作是单线程的
for(int i=0;i<10;i++){
//提交10个线程,每个params装有10条数据
Future<Map<String,String>> futureResult = asyncServiceExecutor.submit(() -> submitSingle(globalId, params));
Map<String,String> resultVOList = futureResult.get();
if(resultVOList != null){
Object commitObject = redisUtils.get(globalId);
if (commitObject == null) {
boolean flag = redisUtils.set(globalId, params.size());
} else {
redisUtils.increment(globalId, (long) params.size());
}
}
}
public Map<String,String> submitSingle(String globalId, JSONArray params) {
try {
log.info("params: {}, size: {}", params, params.size());
Map<String,String> resultVOlist = QueryController.doQuery(params);
return resultVOlist;
} catch (Exception e) {
log.error("QueryController not found error", e);
return new HashMap<>();
}
}
2 可以在线程里面把写redis的逻辑进行整体加锁
public Map<String,String> submitSingle(String globalId, JSONArray params) {
try {
log.info("params: {}, size: {}", params, params.size());
Map<String,String> resultVOlist = QueryController.doQuery(params);
if(redission.lock(uuid)){
Object commitObject = redisUtils.get(globalId);
if (commitObject == null) {
boolean flag = redisUtils.set(globalId, params.size());
} else {
redisUtils.increment(globalId, (long) params.size());
}
}
return resultVOlist;
} catch (Exception e) {
log.error("QueryController not found error", e);
return new HashMap<>();
}
}
总结
- 问题只会越来越多,但也要耐心解决
作为程序员第 115 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha … 


Lyric: 如果难过请你忘了我
这是第4首歌,已经完结了,你们猜出歌名了吗?
- 歌名:借口
- 歌手:周杰伦
- 歌词:周杰伦
- 专辑:七里香
版权声明
本文为[时间是一种解药]所创,转载请带上原文链接,感谢
https://blog.csdn.net/cucgyfjklx/article/details/124338511
边栏推荐
- Solidity: source file structure
- The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
- Installing STEP7 micro / win v4.0 in Windows 10 0 SP9. After installation, you will be prompted with assertion program: pniopcac exe File
- 蓝桥杯练习011
- npm的使用
- Hx711 weight sensor wiring
- DB107-ASEMI整流桥详细数据
- 实验三 FFT及其在卷积计算和谱分析中的应用
- 接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
- 时间戳有什么作用,如何申请?
猜你喜欢

数据库自动备份报错,提示SQLServerAgent 当前未运行,因此无法将此操作通知它。

【操作教程】国标GB28181平台EasyGBS如何开启语音对讲功能?

RT-Thread Studio 工作区编码设置为UTF-8

对于现阶段GameFi发展而言,兼容EVM重要吗?

漫谈同源策略(SOP)和跨域资源共享(CORS)

解决并发问题方案流程及原理总结(表单重复提交问题)

NDSM, CHM, DTM, DEM, DSM, cut continuously, but clear

领域驱动模型DDD(三)——使用Saga管理事务

Times superior AC servo driver settings

Domain driven model DDD (III) -- using saga to manage transactions
随机推荐
redis优化系列(一)基于docker搭建Redis主从
接口测试实战| GET/POST 请求区别详解
三科变频器弯管机参数设置
Web测试需要注意什么?
The number of distinct data in the query table count MySQL Oracle takes the specified number
TCP/IP协议之四TCP协议(上)—理论+实践给你讲清楚
window10安装STEP7 Micro/Win V4.0 SP9,安装完后每次开机都提示Assertion Program:pniopcac.exe File
Test life | less than 2 years after graduation, 0 experience and won the 30W annual salary of a well-known Internet enterprise. How did he do it?
Experiment 3 FFT and its application in convolution calculation and spectrum analysis
Talk about the general problem of substitution between TL431 and az431 (personal experience)
Where is the dimension association table of Flink SQL? 800W dimension table
[untitled]
2022-4-21 [webrtc application] source code analysis of yangrtc / metartc open source library (III)
NLP之TM:基于gensim库调用20newsgr学习doc-topic分布并保存为train-svm-lda.txt、test-svm-lda.txt
Blue Bridge Cup practice 019
3D reconstruction of power conductor based on LIDAR point cloud
国产手机品牌才发现,没有国内消费者支持它们什么也不是
坚持做正确的事情
7-inch touch screen screen calibration parameter setting
国美新动作“真选”“严选”赋能 多维度护航品质消费