码迷,mamicode.com
首页 > 其他好文 > 详细

十一、curator recipes之多锁InterProcessMultiLock

时间:2019-01-16 01:00:56      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:tac   frame   void   []   代码   官方   包含   try   官方文档   

简介

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();
    }
}

 

十一、curator recipes之多锁InterProcessMultiLock

标签:tac   frame   void   []   代码   官方   包含   try   官方文档   

原文地址:https://www.cnblogs.com/lay2017/p/10274969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!