读写锁ReadWriteLock
作者:互联网
写锁和读锁成对出现,保证锁的是同一份资源
写锁正在写入的过程中,读锁获取不到,一直等待
写数据的时候,读实现不可读
import sigma.resource.sync.model.DevopsData;
import sigma.resource.sync.model.JoneData;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class SourceCache {
private static final ReadWriteLock joneReadWriteLock = new ReentrantReadWriteLock();
private static final Lock joneReadLock = joneReadWriteLock.readLock();
private static final Lock joneWriteLock = joneReadWriteLock.writeLock();
/**
* 系统名, jonedata的映射map
*/
private static Map<String,JoneData> joneDataCache;
public static Map<String, JoneData> getJoneDataCache() {
joneReadLock.lock();
try {
return joneDataCache;
}finally {
joneReadLock.unlock();
}
}
public static void setJoneDataCache(Map<String, JoneData> joneDataCache) {
joneWriteLock.lock();
try {
SourceCache.joneDataCache = joneDataCache;
}finally {
joneWriteLock.unlock();
}
}
}
标签:joneDataCache,java,Map,ReadWriteLock,读写,private,static,import 来源: https://www.cnblogs.com/PythonOrg/p/14511477.html