• JAVA线程锁-读写锁应用,简单的缓存系统


    在JAVA1.5版本以后,JAVA API中提供了ReadWriteLock,此类是一个接口,在它的实现类中ReentrantReadWriteLock中有这样一段代码

    class CachedData {
       Object data;
       volatile boolean cacheValid;
       ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
    
       void processCachedData() {
         rwl.readLock().lock();
         if (!cacheValid) {
            // Must release read lock before acquiring write lock
            rwl.readLock().unlock();
            rwl.writeLock().lock();
            // Recheck state because another thread might have acquired
            //   write lock and changed state before we did.
            if (!cacheValid) {
              data = ...
              cacheValid = true;
            }
            // Downgrade by acquiring read lock before releasing write lock
            rwl.readLock().lock();
            rwl.writeLock().unlock(); // Unlock write, still hold read
         }
    
         use(data);
         rwl.readLock().unlock();
       }
     }

    以上代码是一个简单的缓存实现方式,根据以上思路写出如下代码

    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    
    public class CacheSystemTest {
    
        private Map<String, Object> cacheMap = new HashMap<String, Object>();
        
        private ReadWriteLock rwl = new ReentrantReadWriteLock();
        
        /**
         * 为了防止在多线程情景下数据安全问题,需要线程互斥,实现方式是用锁
         * @param key
         * @return
         */
        public Object get(String key){
            rwl.readLock().lock();    //任何一个线程进来后,第一时间加上读锁
            Object obj = null; 
            try{
                obj = cacheMap.get(key);
                if(obj == null){
                    rwl.readLock().unlock();    //在赋值前关闭读锁,并在此检查对象
                    if(obj == null){
                        rwl.writeLock().lock();    //打开一个写锁
                        try{
                            obj = "read write lock";
                        }finally{
                            rwl.writeLock().unlock();    //关闭写锁
                            rwl.readLock().lock();    //恢复正常读锁
                        }
                    }
                }
            }finally{
                rwl.readLock().unlock();
            }
            return obj;
        }
    }

     注:此锁最多支持65535个递归写入锁和读锁,如果试图超出则抛出Error错误

  • 相关阅读:
    centos7 killall 命令
    移动硬盘拒绝访问问题解决方法
    Linux实现内容分发的主备模式的智能DNS
    UWB DWM1000 跟随小车原理--- 原理代码解析
    DWM1000 自动应答代码实现与实例
    UWB DWM1000 跟随小车原理---一张图演示
    DWM1000 帧过滤代码实现
    Bphero-UWB 基站0 和 电脑串口数据格式定义
    DW1000 用户手册中文版 附录3:双向测距(Two-Way Ranging)
    DW1000 用户手册中文版 附录2 IEEE-802.15.4 MAC层
  • 原文地址:https://www.cnblogs.com/duwenlei/p/5110325.html
Copyright © 2020-2023  润新知