<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.0</version>
</dependency>
/**
*
*Curator分布式锁
**/
public class DistributedLock {
private String zkAddr = "127.0.0.1:2181";
private RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
private CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(zkAddr)
.sessionTimeoutMs(2000)
.retryPolicy(retryPolicy)
.build();
public DistributedLock(){
cf.start();
}
//共享不可重入锁
public InterProcessLock getInterProcessSemaphoreMutex(String lockPath){
InterProcessLock ipsmLock = new InterProcessSemaphoreMutex(cf, lockPath);
return ipsmLock;
}
//共享可重入锁
public InterProcessLock getInterProcessMutex(String lockPath){
InterProcessLock ipLock = new InterProcessMutex(cf, lockPath);
return ipLock;
}
//共享可重入读锁
public InterProcessLock getInterProcessReadLock(String lockPath){
InterProcessReadWriteLock ipReadWriteLock = new
InterProcessReadWriteLock (cf, lockPath);
InterProcessLock readLock = ipReadWriteLock.readLock();
return readLock;
}
//共享可重入写锁
public InterProcessLock getInterProcessWriteLock(String lockPath){
InterProcessReadWriteLock ipReadWriteLock =
new InterProcessReadWriteLock(cf, lockPath);
InterProcessLock writeLock = ipReadWriteLock.writeLock();
return writeLock;
}
/**不可重入锁--------begin---------**/
public static void main(String[] args) {
DistributedLock dbLock = new DistributedLock();
new Thread("thread-1"){
@Override
public void run() {
process();
}
}.start();
new Thread("thread-2"){
@Override
public void run() {
process();
}
}.start();
}
private static void process() {
InterProcessLock lock = dbLock.getInterProcessSemaphoreMutex
("/distribute-lock");
System.out.println(Thread.currentThread().getName() + " acquire");
try {
lock.acquire();
System.out.println(Thread.currentThread().getName() + " acquire
success");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(Thread.currentThread().getName() + " release");
try {
lock.release();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " release success");
}
/**不可重入锁--------end---------**/
/**
* 读写锁-------begin-------
* @param args
*/
public static void main(String[] args) {
DistributedLock dbLock = new DistributedLock();
//写锁
for (int i = 0; i < 5; i++) {
new Thread("thread-write-"+i){
@Override
public void run() {
write();
}
}.start();
}
//读锁
for (int i = 0; i < 5; i++) {
new Thread("thread-read-"+i){
@Override
public void run() {
read();
}
}.start();
}
}
// 写入数据
private static void write() throws Exception {
// 创建读写锁对象, Curator 以公平锁的方式进行实现
InterProcessReadWriteLock lock = dbLock.InterProcessReadWriteLock
("/distribute-write-lock");
// 获取写锁(使用 InterProcessMutex 实现, 所以是可以重入的)
InterProcessLock writeLock = lock.writeLock();
writeLock.acquire();
try {
Thread.sleep(10);
testData++;
System.out.println("写入数据 \ t" + testData);
} finally {
writeLock.release();
}
}
// 读取数据
private static void read() throws Exception {
// 创建读写锁对象, Curator 以公平锁的方式进行实现
InterProcessReadWriteLock lock = dbLock.InterProcessReadWriteLock
("/distribute-read-lock");
// 获取读锁(使用 InterProcessMutex 实现, 所以是可以重入的)
InterProcessLock readLock = lock.readLock();
readLock.acquire();
try {
Thread.sleep(10);
System.out.println("读取数据 \ t" + testData);
} finally {
readLock.release();
}
}
// 测试数据变更字段
private Integer testData = 0;
/**
* 读写锁-------end-------
* @param args
*/
}
写入数据 1
写入数据 2
读取数据 2
写入数据 3
读取数据 3
写入数据 4
读取数据 4
读取数据 4
写入数据 5
读取数据 5