@Override protected ReadWriteLock createReadWriteLock(String lockName) { return redisson.getReadWriteLock(lockName); } }
private RReadWriteLock getCachedReadWriteLock(LockType lockType, String key, boolean fair) { String newKey = key + "-" + "fair[" + fair + "]"; RReadWriteLock readWriteLock = readWriteLockMap.get(newKey); if (readWriteLock == null) { RedissonClient redisson = redissonHandler.getRedisson(); RReadWriteLock newReadWriteLock = redisson.getReadWriteLock(key); readWriteLock = readWriteLockMap.putIfAbsent(newKey, newReadWriteLock); if (readWriteLock == null) { readWriteLock = newReadWriteLock; } } return readWriteLock; } }
public static void main(String[] args) throws InterruptedException { // connects to 127.0.0.1:6379 by default RedissonClient redisson = Redisson.create(); final RReadWriteLock lock = redisson.getReadWriteLock("lock"); lock.writeLock().tryLock(); Thread t = new Thread() { public void run() { RLock r = lock.readLock(); r.lock(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } r.unlock(); }; }; t.start(); t.join(); lock.writeLock().unlock(); t.join(); redisson.shutdown(); }
/** 根据完整的锁名获取锁 */ private RLock getLock(String lockNameFull, EcLockTypeEnum lockType) { if (EcLockTypeEnum.isFair(lockType)) { return redisson.getFairLock(lockNameFull); } else if (EcLockTypeEnum.isUnfair(lockType)){ return redisson.getLock(lockNameFull); } else if (EcLockTypeEnum.isRead(lockType)) { return redisson.getReadWriteLock(lockNameFull).readLock(); } else if (EcLockTypeEnum.isWrite(lockType)) { return redisson.getReadWriteLock(lockNameFull).writeLock(); } throw new EcBaseBusinessException(EcLockErrorCodeEnum.LOCK_TYPE_NOT_SUPPORT); }
@Override public boolean acquire() { try { rLock=redissonClient.getReadWriteLock(lockInfo.getName()); return rLock.readLock().tryLock(lockInfo.getWaitTime(), lockInfo.getLeaseTime(), TimeUnit.SECONDS); } catch (InterruptedException e) { return false; } }
@Override public boolean acquire() { try { rLock=redissonClient.getReadWriteLock(lockInfo.getName()); return rLock.writeLock().tryLock(lockInfo.getWaitTime(), lockInfo.getLeaseTime(), TimeUnit.SECONDS); } catch (InterruptedException e) { return false; } }