业务背景:
系统启动时需要向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;
}
public ModelConfigResource getModelResource() {
if (modelConfigResource == null) {
lock.lock();
if (modelConfigResource != null) {
return modelConfigResource;
}
ModelConfigResource modelConfig = new ModelConfigResource();
DRMClient.getInstance().register(modelConfig);
modelConfigResource = modelConfig;
lock.unlock();
}
return modelConfigResource;
}
}
出现问题的流程:
a. 绿色的线程为第一个profile注册的线程,蓝色的线程为第二个profile注册的线程,并发请求进入。
b. 绿色实心为执行到的流程,蓝色实心为执行到的流程,白色为未执行到的流程。
c. 绿色prfoile线程进入后,执行到黄色代码段时,蓝色profile线程进入,此时profile中还没有值,因此会去执行lock.lock,因为lock被绿色线程所使用,所以等待此锁释放。
d. 绿色profile线程执行完,此时profile已经被赋值,蓝色线程执行lock.lock,获得锁,继续向下执行,执行到判断逻辑profile!=null, 此时不为null,直接return。
e. 此时蓝色线程中ReentrantLock锁是lock状态,此时锁没有被unlock,模型阀门配置想要获取锁注册资源时,会出现此锁一直是lock状态,无法获取此锁,导致一直无法注册资源。
修复后的代码:
public class CommonConfig {
/** 非公平锁 */
private static ReentrantLock lock = new ReentrantLock(false);
/** profile相关阀门控制*/
private ProfileConfigResource profileConfigResource;
/** 模型相关阀门控制*/
private ModelConfigResource modelConfigResource;
public ProfileConfigResource getProfileResource() {
if (profileConfigResource == null) {
try {
lock.lock();
if (profileConfigResource != null) {
return profileConfigResource;
}
ProfileConfigResource profileConfig = new ProfileConfigResource();
DRMClient.getInstance().register(profileConfig);
profileConfigResource = profileConfig;
} finally {
lock.unlock();
}
}
return profileConfigResource;
}
public ModelConfigResource getModelResource() {
if (modelConfigResource == null) {
try {
lock.lock();
if (modelConfigResource != null) {
return modelConfigResource;
}
ModelConfigResource modelConfig = new ModelConfigResource();
DRMClient.getInstance().register(modelConfig);
modelConfigResource = modelConfig;
} finally {
lock.unlock();
}
}
return modelConfigResource;
}
}