ReentrantLock修饰类文件,实现按类获取锁的逻辑

1.ReentrantLock 给类文件加锁,实现类似synchronized(class)的功能

核心是类文件中,使用static修饰的reentrantLock对象

public class SourceCache {

    public static final Lock reentrantLock = new ReentrantLock();

    /**
* 系统名,devopsdata的映射map
*/
private static Map<String,DevopsData> devopsDataCache; /**
* 系统名, jonedata的映射map
*/
private static Map<String,JoneData> joneDataCache; public static Map<String, DevopsData> getDevopsDataCache() {
return devopsDataCache;
} public static Map<String, JoneData> getJoneDataCache() {
return joneDataCache;
} public static void setDevopsDataCache(Map<String, DevopsData> devopsDataCache) {
reentrantLock.lock();
try {
SourceCache.devopsDataCache = devopsDataCache;
}finally {
reentrantLock.unlock();
}
} public static void setJoneDataCache(Map<String, JoneData> joneDataCache) {
reentrantLock.lock();
try {
SourceCache.joneDataCache = joneDataCache;
}finally {
reentrantLock.unlock();
}
} }

2.加锁,执行写入,解锁三部曲

reentrantLock.lock();
try {
SourceCache.joneDataCache = joneDataCache;
}finally {
reentrantLock.unlock();
}
上一篇:Asp.Net Core 开发之旅之NLog日志


下一篇:github之无命令可视化界面操作——GitHub DeskTop