zoukankan      html  css  js  c++  java
  • zookeeper curator学习(分布式锁)

    
    

    基础知识: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();
            }
        }
        
    }
  • 相关阅读:
    常用的正则表达式
    vue多页面应用
    webpack + jquery + bootstrap 环境配置
    Goroutine的几个例子
    设置css通用字体
    简单的gulpfile.js参数配置
    1:时间戳转换成年月日函数,2:url截取参数方法,3:弹窗自定义方法 4:点击按钮加入购物车
    github上比较全的知识
    秒杀倒计时
    正则校验手机号码并获取手机验证码倒计时的实例
  • 原文地址:https://www.cnblogs.com/zhangjunqing/p/7823626.html
Copyright © 2011-2022 走看看