当前位置:网站首页>Multithreading uses redis to accumulate. The result is incorrect. Solution
Multithreading uses redis to accumulate. The result is incorrect. Solution
2022-04-22 16:36:00 【Time is an antidote】
multithreading redis The result of accumulation is not the right solution
The problem background
In the use of multithreading redis Of value An error occurred while accumulating , Use pseudo code for cause analysis
for(int i=0;i<10;i++){
// Submit 10 Threads , Every params Equipped with 10 Data
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);
// Because it is multi-core , It can achieve real concurrency at the same time , Therefore, there may be 5 Threads arrive at this statement at the same time
// For the first time at the same time ,redis There is no such thing in it globalId keyword , therefore 5 All threads are for null
Object commitObject = redisUtils.get(globalId);
// When there is 5 A for null After the thread comes in , It will cause the same key to be created multiple times , Accumulating data is equivalent to losing 4 Time , That is to say 40 strip
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<>();
}
}
Solution
1 Since it is the cause of multithreading , Then we can write redis The write operation of is moved out to the main thread ,redis The underlying write operation is single threaded
for(int i=0;i<10;i++){
// Submit 10 Threads , Every params Equipped with 10 Data
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 You can write... In the thread redis The logic of is locked as a whole
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<>();
}
}
summary
- There will only be more and more problems , But be patient
As a programmer 115 An article , Write one line of lyrics at a time and record , Look at how many songs there are in life ,wahahaha … 


Lyric: If you are sad, please forget me
This is the first 4 song , It's over , Did you guess the title of the song ?
- Da da da : excuse
- singer : Jay Chou
- The lyrics : Jay Chou
- Album : Qi li xiang
版权声明
本文为[Time is an antidote]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221629553335.html
边栏推荐
- RTP packaging and unpacking of webrtc
- Experiment 4: KNN, naive Bayes of data mining
- numpy基础大全(创建、索引、常用函数)
- 8.1 序列模型
- 1016 phone bills (25 points) test point 1,2
- 两个有序链表合并(相同内容删去)
- Random talk on homology strategy (SOP) and cross domain resource sharing (CORS)
- Experiment 3 FFT and its application in convolution calculation and spectrum analysis
- Blue Bridge Cup exercise 015
- How to select one of the two lines that coincide in SolidWorks
猜你喜欢

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

Summary of process and principle of solution to concurrency problem (form repeated submission problem)

【ARM汇编】如何对键入数据做判断?

Niu Ke SQL question brushing record

This API hub is powerful. It contains open APIs such as nailing enterprise wechat, and can be debugged directly!

产业调研:厂商视角看国产数据库发展趋势

这个API Hub厉害了,收录了钉钉企业微信等开放Api,还能直接调试 !

C# ODBC将一个文件夹的文件装载到ORACLE数据库BLOB列,并将BLOB列下载到另一个文件夹

1016 phone bills (25 points) test point 1,2

MINIUSB pin interface pin definition
随机推荐
In SolidWorks, why can I only set direction 1 but not direction 2 for linear sketch array?
谈谈TL431与AZ431代换通用问题(个人经验)
接口协议之抓包分析 TCP 协议
动态拖动两个div的宽度
TCP / IP protocol IV TCP protocol (I) - Theory + practice to make it clear to you
ASEMI低压降肖特基二极管比普通肖特基好在哪?
MiniUSB管脚接口引脚定义
【csnote】范式
31岁拿了阿里P6的offer,还有必要去吗?
Shiro customizes the cache and extends the Shiro cache module. You only need to configure the cache to realize session sharing
12.88万元的小魔驼2.0,毫末智行托起末端物流自动配送的商用梦想
UI测试有哪些功能问题?-泽众云测试
Where is the dimension association table of Flink SQL? 800W dimension table
Use of serial port data plot serialplot
小练习:二分查找及实现
Insist on doing the right thing
Blue Bridge Cup practice 020
Blue Bridge Cup practice 011
Domain driven model DDD (III) -- using saga to manage transactions
蓝桥杯练习019