分布式重入读写共享锁:InterProcessReadWriteLock共享重入读写锁zk分布式锁基础依赖
package com.miku.common.util.lock.zookeeper;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import java.util.concurrent.ConcurrentHashMap;
/**
* 分布式共享重入读写锁<ul>
* <li>1. 互斥: 读写锁保持一对相关的锁,一个用于只读操作,另一个用于写入。只要没有写入者,读取锁可以同时由多个读取者进程保存。写锁是独占的。</li>
* <li>2. 重入(Reentrancy): 此锁允许读和写以重入锁的形式重新获取读或写锁。在写入线程/进程持有的所有写入锁定被释放之前,不允许非重入式读取器。
* 另外,写锁可以获取读锁,但是反之亦然。如果读锁试图获得写入锁,它将永远不会成功。</li>
* <li>3. 写锁降级: 锁定降级重入也允许从写入锁定降级到读取锁定,通过获取写入锁定,然后读取锁定,然后释放写入锁定。但是,从读锁升级到写锁是不可能的。</li>
* <li>4. 公平互斥量: 每个客户端将按请求的顺序(从ZK的角度来看)获得互斥量,即每次请求监听上一个节点。</li>
* <li>5. 根据锁类型(zk路径)维持全局唯一实例</li>
* </ul>
*
* @author:panw.
*/
public class SharedReentrantReadWriteLock extends SharedReentrantLock {
public static ConcurrentHashMap<String,Object> locks = new ConcurrentHashMap<>();
private final String lockKey = "read-write-lock";
private final String readLock = "read-lock";
private final String writeLock = "write-lock";
// 获取锁超时时间
private final long time = 5000L;
/**
* Shared Reentrant Read Lock<ul>
* <li>1. 获取可重入读锁,zk锁路径,路径相同锁相同,默认获取超时时间5000毫秒。</li>
* <li>2. 实现细节{@link com.miku.common.util.lock.zookeeper.SharedReentrantLock#acquire(String, long)}。</li>
* <li>3. 多个客户端读锁共享。</li>
* </ul>
*
* @param var
* @return 如果获得了读锁,则返回true,否则返回false
* @throws IllegalStateException 读锁获取失败
*/
public boolean readLock(String var) {
try {
return super.lock(getReadLock(var),time);
} catch (Exception e) {
throw new IllegalStateException("获取读锁失败=>" + e);
}
}
public boolean readLock(String var,final long time) {
InterProcessMutex interProcessMutex = getReadLock(var);
try {
return super.lock(getReadLock(var),time);
} catch (Exception e) {
throw new IllegalStateException("获取读锁失败=>" + e);
}
}
/**
* Shared Reentrant Write Lock
* <li>1. 获取可重入写锁,zk锁路径,路径相同锁相同,默认获取超时时间5000毫秒。</li>
* <li>2. 实现细节{@link com.miku.common.util.lock.zookeeper.SharedReentrantLock#acquire(String, long)}。</li>
* <li>3. 写锁独占,多个客户端无法同时获取写锁,写锁可降级为读锁。</li>
* </ul>
*
* @param var
* @return
*/
public boolean writeLock(String var) {
InterProcessMutex interProcessMutex = getWriteLock(var);
try {
return super.lock(getWriteLock(var),time);
} catch (Exception e) {
throw new IllegalStateException("获取写锁失败=>" + e);
}
}
public boolean writeLock(String var,final long time) {
InterProcessMutex interProcessMutex = getWriteLock(var);
try {
return super.lock(getWriteLock(var),time);
} catch (Exception e) {
throw new IllegalStateException("获取写锁失败=>" + e);
}
}
public void readRelease(String var) throws Exception {
super.unlock(getReadLock(var));
}
public void writeRelease(String var) throws Exception {
super.unlock(getWriteLock(var));
}
private InterProcessReadWriteLock getReadWriteLock(String var){
String key = lockKey + var;
InterProcessReadWriteLock interProcessReadWriteLock = null;
if (locks.containsKey(key)){
interProcessReadWriteLock = (InterProcessReadWriteLock) locks.get(key);
} else {
interProcessReadWriteLock = new InterProcessReadWriteLock(curatorFramework,var);
locks.put(key,interProcessReadWriteLock);
}
return interProcessReadWriteLock;
}
private InterProcessMutex getReadLock(String var){
String readLockKey = readLock + var;
InterProcessMutex interProcessMutex = null;
if (locks.containsKey(readLockKey)){
interProcessMutex = (InterProcessMutex) locks.get(readLockKey);
} else {
interProcessMutex = getReadWriteLock(var).readLock();
locks.put(readLockKey,interProcessMutex);
}
return interProcessMutex;
}
private InterProcessMutex getWriteLock(String var){
String writeLockKey = writeLock + var;
InterProcessMutex interProcessMutex = null;
if (locks.containsKey(writeLockKey)){
interProcessMutex = (InterProcessMutex) locks.get(writeLockKey);
} else {
interProcessMutex = getReadWriteLock(var).writeLock();
locks.put(writeLockKey,interProcessMutex);
}
return interProcessMutex;
}
}
结语:如有觉得不够力,不够用的请留言注明!!
博客原创:写作不易,转载请标明出处。文章地址:文章地址(๑˃∀˂๑)♪阿里嘎多(๑˃∀˂๑)♪
分布式锁-基于zookeeper客户端curator实现锁机制(一)锁基础
分布式锁-zookeeper-SharedLock基于InterProcessSemaphoreMutex(二)分布式共享锁
分布式锁-zookeeper-SharedReentrantLock基于InterProcessMutex(三)分布式重入共享锁
分布式锁-zookeeper-SharedReentrantReadWriteLock基于InterProcessReadWriteLock(四)分布式重入读写共享锁