业务背景:
系统启动时需要向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