zoukankan      html  css  js  c++  java
  • 十一、curator recipes之联锁InterProcessMultiLock

    简介

    curator实现了一个类似容器的锁InterProcessMultiLock,它可以把多个锁包含起来像一个锁一样进行操作,简单来说就是对多个锁进行一组操作。当acquire的时候就获得多个锁资源,否则失败。当release时候释放所有锁资源,不过如果其中一把锁释放失败将会被忽略。

    官方文档:http://curator.apache.org/curator-recipes/multi-shared-lock.html

    javaDoc:http://curator.apache.org/apidocs/org/apache/curator/framework/recipes/locks/InterProcessMultiLock.html

    代码示例

    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.recipes.locks.InterProcessLock;
    import org.apache.curator.framework.recipes.locks.InterProcessMultiLock;
    import org.apache.curator.framework.recipes.locks.InterProcessMutex;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MultiLockDemo {
        private static CuratorFramework       client  = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 2));
        private static String                 path1   = "/mutex/001";
        private static String                 path2   = "/mutex/002";
        private static List<InterProcessLock> mutexes = new ArrayList<>();
        private static InterProcessMutex      mutex1;
        private static InterProcessMutex      mutex2;
    
        static {
            mutex1 = new InterProcessMutex(client, path1);
            mutex2 = new InterProcessMutex(client, path2);
            mutexes.add(mutex1);
            mutexes.add(mutex2);
            client.start();
        }
    
        public static void main(String[] args) throws Exception {
            InterProcessMultiLock multiLock = new InterProcessMultiLock(mutexes);
            multiLock.acquire();
            System.out.println("acquired multi lock");
            startThread0();
            startThread1();
            Thread.sleep(5000);
            multiLock.release();
            System.out.println("release multi lock");
            Thread.sleep(50000);
            client.close();
        }
    
        public static void startThread0() throws InterruptedException {
            Thread.sleep(1000);
            new Thread(() -> {
                try {
                    System.out.println("thread0 acquring");
                    mutex1.acquire();
                    System.out.println("thread0 acquired");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        mutex1.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    
        public static void startThread1() throws InterruptedException {
            Thread.sleep(1000);
            new Thread(() -> {
                try {
                    System.out.println("thread1 acquiring");
                    mutex2.acquire();
                    System.out.println("thread1 acquired");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        mutex2.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
  • 相关阅读:
    二维码
    文件下载
    相对路径使用的特殊符号
    Httpclient的使用
    MySQL比like语句更高效的写法
    jQuery中turn.js(翻页效果)学习笔记
    如何在忘记mysql的登录密码时更改mysql登录的密码(window及linux)
    详细介绍svn在eclipse中的使用(附图解说明)
    Xshell6远程访问linux及Xftp6远程针对linux系统中文件操作(附图文详解)
    利用workbench对linux/Ubuntu系统中的mysql数据库进行操作
  • 原文地址:https://www.cnblogs.com/lay2017/p/10274969.html
Copyright © 2011-2022 走看看