十curator recipes之多锁InterProcessMultiLock

Posted lay2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十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();
    }
}

 

以上是关于十curator recipes之多锁InterProcessMultiLock的主要内容,如果未能解决你的问题,请参考以下文章

十九curator recipes之PathChildrenCache

二十curator recipes之NodeCache

curator recipes之屏障barrier

七curator recipes之阻塞队列SimpleDistributedQueue

九curator recipes之不可重入锁InterProcessSemaphoreMutex

八curator recipes之选举主节点LeaderSelector