简介

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的更多相关文章

  1. 九、curator recipes之不可重入锁InterProcessSemaphoreMutex

    简介 recipes的InterProcessSemaphoreMutex是一种不可重入的互斥锁,也就意味着即使是同一个线程也无法在持有锁的情况下再次获得锁,所以需要注意,不可重入的锁很容易在一些情况 ...

  2. 四、curator recipes之共享重入互斥锁

    简介 curator的recipes实现了可重入互斥锁,允许你在分布式场景下多个进程之间实现锁的互斥以协调多进程执行. 相关类:InterProcessMutex 官方文档:http://curato ...

  3. 二十一、curator recipes之TreeCache

    简介 curator的TreeCache允许对某个路径的数据和路径变更以及其下所有子孙节点的数据和路径变更进行监听. 官方文档:http://curator.apache.org/curator-re ...

  4. curator教程二——分布式锁

    简介   在分布式环境下,为了防止多个服务同时修改同一个值,出现数据同步问题,通常用redis和zookeeper做分布式锁,在这里我们用zookeeper做分布式锁,并和单点环境中ReenTranL ...

  5. 三、curator recipes之共享的可重入读写锁

    简介 curator实现了跨JVM的可重入读写互斥锁.它使用zookeeper去进行加锁,所以指定相同路径的处理线程将会基于“公平锁”的机制去竞争锁资源. 读写锁包含了读锁.写锁两个,它们的互斥关系如 ...

  6. curator 实现分布式一致性锁

    最近准备在项目中引入分布式锁,故而研究基于zookeeper的curator框架. 网上资料不多,自己研究其源码发现,这个框架已经帮我做了很多现成的实现. 下面介绍下锁的实现: 通过源码中Lockin ...

  7. 二十、curator recipes之NodeCache

    简介 Curator的NodeCache允许你监听一个节点,当节点数据更改或者节点被删除的时候将会触发监听. 官方文档:http://curator.apache.org/curator-recipe ...

  8. 十九、curator recipes之PathChildrenCache

    简介 curator可以监听路径下子节点的变更操作,如创建节点,删除节点 官方文档:http://curator.apache.org/curator-recipes/path-cache.html ...

  9. 十八、curator recipes之DistributedDelayQueue

    简介 curator实现了类似DelayQueue的分布式延迟队列 官方文档:http://curator.apache.org/curator-recipes/distributed-delay-q ...

随机推荐

  1. linux中使用unzip命令中文乱码解决办法

    今天在使用unzip进行解压缩文件时,发现解压出的文件中文乱码,最后使用如下命令解决: unzip -O CP936 xxx.zip 特此记录一下.

  2. Linux mint 安装踩坑记录

    记得之前电脑上的那个Ubuntu是去年寒假的时候安装的,算下来自己用Linux也快一年了.虽然在去年暑假的时候我也曾经想过要把Ubuntu升级到18.04可是当时安装了几次都没有成功,自己也就放弃了. ...

  3. 【OCP 12c】最新CUUG OCP-071考试题库(63题)

    63.(22-4) choose the best answer: View the Exhibit and examine the data in the PRODUCTS table. Which ...

  4. AI下载步骤

    ai下载地址:https://www.adobe.com/cn/creativecloud/catalog/desktop.html?promoid=PTYTQ77P&mv=other 破解器 ...

  5. button不能添加伪类元素

    今日试了一下button添加伪类元素,结果是不行的前后都叠加在一起 html代码: <button class="form_btn" formType="submi ...

  6. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  7. [Objective-C语言教程]扩展(30)

    类扩展与类别有一些相似之处,但它只能添加到编译时具有源代码的类中(类与类扩展同时编译). 类扩展声明的方法是在原始类的实现块中实现的,因此不能在框架类上声明类扩展,例如Cocoa或Cocoa Touc ...

  8. Swagger2使用记录

    1. Swagger2使用记录 1.1. Bean配置文件 @Configuration public class Swagger2 { @Bean public Docket createRestA ...

  9. net 反编译神器

    文章地址:https://www.cnblogs.com/sheng-jie/p/10168411.html dnSpy官网下载  分享链接 .net core源码导航 https://www.cnb ...

  10. 【实战】某项目SQL注入引发的思考

    数据包: 测试参数:username,测试payload: ' ' or '1'='1 ' or '1'='2 响应结果都未发生任何变化,借助sqlmap测试,结果一样: 尝试在or前面进行简单的fu ...