并发情况下ReentrantLock死锁

在系统向DRM服务器注册资源过程中,由于并发操作,可能导致ReentrantLock出现死锁。当两个线程分别尝试注册不同资源时,一个线程在持有锁的情况下等待另一个已持有的锁,造成死锁。修复方法在于避免在持有锁的状态下进行可能导致阻塞的操作,确保锁的正确释放,从而防止死锁的发生。
摘要由CSDN通过智能技术生成

业务背景:

系统启动时需要向DRM服务器注册资源,同一个资源只能注册一次,不能重复注册。现在有两个资源需要注册,第一个资源为Profile阀门控制,第二个资源为模型阀门控制。

存在缺陷的代码:

public class CommonConfig {

    /** 非公平锁 */
    private static ReentrantLock  lock   = new ReentrantLock(false);

    /** profile相关阀门控制*/
    private ProfileConfigResource profileConfigResource;

    /** 模型相关阀门控制*/
    private ModelConfigResource   modelConfigResource;

    public ProfileConfigResource getProfileResource() {
        if (profileConfigResource == null) {
            lock.lock();
            if (profileConfigResource != null) {
                return profileConfigResource;
            }
            ProfileConfigResource profileConfig = new ProfileConfigResource();
            DRMClient.getInstance().register(profileConfig);
            profileConfigResource = profileConfig;
            lock.unlock();
        }
        return profileConfigResource
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值