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

 

posted @ 2019-01-16 00:24  __lay  阅读(929)  评论(0编辑  收藏  举报