可重入性是指线程在持有锁的情况下再次请求加锁,如果一个锁支持同一个线程的多次加锁,那么这个锁就是可重入的,redis分布式锁如果要支持可重入,需要对客户端的set方法进行包装,使用线程的ThreadLocal变量存储当前持有锁的计数
public class RedisWithReentrantLock{ private ThreadLocal> lockers = new ThreadLocal<>(); private Jedis jedis; public RedisWithReentrantLock(Jedis jedis){ this.jedis = jedis; } private boolean _lock(String key){ return jedis.set(key,"","nx","ex",5L) != null; } private void _unlock(String key){ jedis.del(key); } private Map currentLockers(){ Map refs = lockers.get(); if(refs != null) return refs; lockers.set(new HashMap<>()); return lockers.get(); } public boolean lock(String key){ Map refs = currentLockers(); Integer refCnt = refs.get(key); if(refCnt != null){ refs.put(key,refCnt+1); return true; } boolean ok = this._lock(key); if(!ok){ return false; } refs.put(key,1); return true; } public boolean unlock(String key){ Map refs = currentLockers(); Integer refCnt = refs.get(key); if(refCnt == null){ return false; } refCnt-=1; if(refCnt>0){ refs.put(key,refCnt); } else{ refs.remove(key); this._unlock(key); } return true; } }
上一篇 Android网络操作-2021-10-22
下一篇 Springboot使用Mybatisplus:BaseMapper与IService
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号