多线程对全局Map做computeIfAbsent()引发的线程问题

1,问题描述

前面通过异步线程方式解决了zk通知消息丢失的问题,当是又发生了引起的新的问题。投产生产10天以来发生了3次,每天数小时 缓存参数重复导致改参数不可用的问题,每次运行一段时间又会自动恢复。很是诡异。问题分析过程:最近只修改了 缓存刷新方法 改为异步,再结合 日志中发生时间 都存在间隔 zk并发通知情况,所以分析可能是scm缓存刷新方法。查看 缓存刷新逻辑如下:

2,缓存刷新逻辑

复制代码
static Map xxMap = new concureenMap()


refresh()
{

1,对全局map进行清理
xxMap.clear();
2.从数据库加载数据
List lxxx = dao.query();
3.遍历lxx逐条往xxMap中 添加 缓存数据
xxxx
PARAM_PARAM_CACHE.computeIfAbsent(paramParam.getCode(), LambdaUtil.newArrayList()).add(paramParam.getReleCode());
....

}

3,问题分析

可以从上面代码看出如果,存在并发操作 全局map。再看 refresh()方法,当多个线程同时操作时,可能会发生。A、B线程同时走完 步骤1 然后到 2、3 步骤,就会造成 同样的数据 多次 执行

PARAM_PARAM_CACHE.computeIfAbsent(paramParam.getCode(), LambdaUtil.newArrayList()).add(paramParam.getReleCode());方法

4,问题解决

采用synchronized 关键字 让 refresh()方法 变更同步,并且实际业务中加载缓存也是非频繁的。

复制代码
refresh()
{
synchronized(XX.class)
{
    1,对全局map进行清理
    xxMap.clear();
    2.从数据库加载数据
    List lxxx = dao.query();
    3.遍历lxx逐条往xxMap中 添加 缓存数据
    xxxx
    PARAM_PARAM_CACHE.computeIfAbsent(paramParam.getCode(),         
      LambdaUtil.newArrayList()).add(paramParam.getReleCode());
    ....
}

}

4,单元测试代码,使用栅栏模拟10个线程同时调用缓存刷新方法,map中会出现多个相同 值。

复制代码
//定义一个线程池,核心线程数为10
private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
EntityRelation paramParam = new EntityRelation();
@Test
public void testB() {
    //EntityRelation{id=12385,type=param-param,code=QZFceshi,releCode=idNo,seq=0, opType=INSERT, opTime=Fri Jan 19 15:48:44 CST 2024, operator=23080386}
 Map<String, List<String>> PARAM_PARAM_CACHE = new ConcurrentHashMap<>();
 paramParam.setCode("QZFceshi");
 paramParam.setReleCode("releCode");
 PARAM_PARAM_CACHE.computeIfAbsent(paramParam.getCode(), LambdaUtil.newArrayList()).add(paramParam.getReleCode());
 PARAM_PARAM_CACHE.computeIfAbsent(paramParam.getCode(), LambdaUtil.newArrayList()).add(paramParam.getReleCode());
 PARAM_PARAM_CACHE.computeIfAbsent(paramParam.getCode(), LambdaUtil.newArrayList()).add(paramParam.getReleCode());


 EntityRelationCache erc = SpringContextUtil.getBeanFactory().getBean(EntityRelationCache.class);
 //erc.refresh("code");
 //定义栅栏,大小必须小于线程池分配线程数,我这里 projects.size()比较小
 /*CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
 try {
 for (int i=0;i<10;i++)
 {
 fixedThreadPool.submit(()->
 {
 try
 {
 //业务代码,这块根据不同的需求来写。
 erc.refresh("code");
 }catch (Exception e){
 e.printStackTrace();
 }finally {
 try {
 //线程执行完后阻塞,所有线程阻塞的之后,解放
 cyclicBarrier.await();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 });
 }
 } finally {
 //主线程阻塞
 try {
 cyclicBarrier.await();
 } catch (InterruptedException e) {
 throw new RuntimeException(e);
 } catch (BrokenBarrierException e) {
 throw new RuntimeException(e);
 }
 }
}
相关推荐
码农研究僧9 分钟前
Ubuntu 磁盘空间占用清理(宝塔)
linux·运维·ubuntu
{⌐■_■}9 分钟前
【计网】认识跨域,及其在go中通过注册CORS中间件解决跨域方案,go-zero、gin
java·linux·开发语言·c++·中间件·golang·gin
若疆赤云online12 分钟前
window和ubuntu自签证书
linux·运维·ubuntu
像风一样自由202014 分钟前
图像识别系统 - Ubuntu部署指南(香橙派开发板测试)-学习记录1
linux·学习·ubuntu
yangshuo128115 分钟前
新ubuntu物理机开启ipv6让外网访问
linux·运维·ubuntu
ZZZKKKRTSAE31 分钟前
快速上手Linux的Web服务器的部署及优化
linux·运维·服务器·web
csdn_aspnet1 小时前
如何在 Windows 10 中使用 WSL 和 Debian 安装 Postgresql 和 Postgis
windows·postgresql·debian
小梦想的博客1 小时前
将本地Springboot项目部署到Linux服务器
服务器
卷卷的小趴菜学编程1 小时前
Linux系统之----程序地址空间
linux·运维·虚拟地址空间·写时拷贝·程序地址空间·缺页中断·惰性加载
九鼎科技-Leo1 小时前
写windows服务日志-.net4.5.2-定时修改数据库中某些参数
数据库·windows·.net