zookeeper curator 可重入与不可重入分布式锁

转载自https://www.cnblogs.com/zhangjunqing/p/7823626.html

基础知识:http://www.cnblogs.com/LiZhiW/p/4931577.html

项目路径:https://gitee.com/zhangjunqing/spring-boot

1 可重入读写锁示例代码如下(lock.acquire加几个,就必须使用几个lock.release()释放):

package com.topsec.lock;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 测试可重入锁(可以多次获得锁不会被阻塞,释放时也需释放多把锁)
 * @author 76524
 *
 */
public class SharedReentrantLock1  implements Runnable{
    private InterProcessMutex lock;//可重入锁实现类
    private String lockPAth = "/lock/shareLock";
    private int i;
    private String clientName;
    //zookeeper集群地址
    public static final String ZOOKEEPERSTRING = "192.168.99.129:2181,192.168.99.153:2181,192.168.99.171:2181";
    
    public SharedReentrantLock1(CuratorFramework client,String clientName) {
        lock = new InterProcessMutex(client, lockPAth);
        this.clientName = clientName;
    }
    
    public void run() {
        try {
            Thread.sleep((new java.util.Random().nextInt(2000)));
            lock.acquire();  //增加第一把锁
            if(lock.isAcquiredInThisProcess()) {
                System.out.println(clientName + " 获得锁");
                i++;
            }
            lock.acquire();  //增加第二把锁
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                
                System.out.println(clientName+"释放第一把锁");
                lock.release();
                System.out.println(clientName+"释放第二把锁");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    public static void main(String[] args) {
        CuratorFramework client = CuratorFrameworkFactory.newClient(ZOOKEEPERSTRING, new ExponentialBackoffRetry(1000, 3));
        client.start();
        //启动100个线程进行测试
        for(int i = 0;i<100;i++) {
            SharedReentrantLock1 sharedReentrantLock = new SharedReentrantLock1(client, "第"+i+"个客户端:");
            Thread thread = new Thread(sharedReentrantLock);
            thread.start();
        }
    }
    
}

2:不可重入锁示例代码如下(lock.acquire加几个都只相当于加一个):

package com.topsec.lock;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
/**
 * 测试不可重入锁(只能获得一次锁,使用完后释放)
 * @author 76524
 *
 */
public class SharedNOReentrantLock   implements Runnable{
    private InterProcessSemaphoreMutex lock;//不可重入锁
    private String lockPAth = "/lock/shareLock";
    private int i;
    private String clientName;
    //zookeeper集群地址
    public static final String ZOOKEEPERSTRING = "192.168.99.129:2181,192.168.99.153:2181,192.168.99.171:2181";
    
    public SharedNOReentrantLock(CuratorFramework client,String clientName) {
        lock = new InterProcessSemaphoreMutex(client, lockPAth);
        this.clientName = clientName;
    }
    
    public void run() {
        try {
            Thread.sleep((new java.util.Random().nextInt(2000)));
            lock.acquire();  //增加第一把锁
            if(lock.isAcquiredInThisProcess()) {
                System.out.println(clientName + " 获得锁");
                i++;
            }
            lock.acquire();  //增加第二把锁这个锁相当于不起作用
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
//                lock.release();
                System.out.println(clientName+"释放第一把锁");
                lock.release();
                System.out.println(clientName+"释放第二把锁");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    public static void main(String[] args) {
        CuratorFramework client = CuratorFrameworkFactory.newClient(ZOOKEEPERSTRING, new ExponentialBackoffRetry(1000, 3));
        client.start();
        for(int i = 0;i<100;i++) {
            SharedReentrantLock2 sharedReentrantLock = new SharedReentrantLock2(client, "第"+i+"个客户端:");
            Thread thread = new Thread(sharedReentrantLock);
            thread.start();
        }
    }
    
}

ps:最后的基于信号量的不可重入锁,看其构造方法其实maxLeases的值设置为1,这样每次只能有一个获取到锁,其他等待其释放lease后才能获取到锁。


展开阅读全文

没有更多推荐了,返回首页