<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