读写锁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();
        }
    }
}

posted @ 2021-03-10 14:31  SpecialSpeculator  阅读(78)  评论(0编辑  收藏  举报