文档章节

curator 锁之 Shared Reentrant Lock

y
 yiqifendou
发布于 2016/10/08 21:15
字数 806
阅读 91
收藏 0

「深度学习福利」大神带你进阶工程师,立即查看>>>

1、描述
共享锁:全局同步分布式锁,同一时间两台机器只能有一台获得锁

2、参与类
InterProcessMutex

3、用法

3.1 创建

InterProcessMutex 实例

public InterProcessMutex(CuratorFramework client,
                         String path)
Parameters:
client - client
path - the path to lock

3.2 一般用法

用其中一个方法,获取锁

public void acquire()
Acquire the mutex - blocking until it's available. Note: the same thread can call acquire
re-entrantly. Each call to acquire must be balanced by a call to release()

在锁可用之前会获取互斥锁会一直阻塞。注意:同一个线程可用调用 acquire 重新参与争抢锁资源.每次调用都要调 release() 平衡。

public boolean acquire(long time,
                       TimeUnit unit)
Acquire the mutex - blocks until it's available or the given time expires. Note: the same thread can
call acquire re-entrantly. Each call to acquire that returns true must be balanced by a call to release()

Parameters:
time - time to wait
unit - time unit
Returns:
true if the mutex was acquired, false if not

该方法在锁可用或者超时之前会阻塞. 返回 true 表示已获取到锁.
调用 release() 释放互斥体

public void release()
Perform one release of the mutex if the calling thread is the same thread that acquired it. If the
thread had made multiple calls to acquire, the mutex will still be held when this method returns.

如果是执行 acquired 操作的线程调用该方法会释放一个互斥体.但如果一个线程已经多次调用 acquired 操作,那当该方法返回时,互斥体会依然被保留。
NOTE: A InterProcessMutex instance is reusable. i.e. don't create a new instance every time. Re-use a single instance.
注意:InterProcessMutex 实例 是可重用的.不用每次都创建一个新的实例. 重用一个单实例.

4、撤销
InterProcessMutex supports a cooperative revocation mechanism as described on the ZooKeeper recipes wiki
InterProcessMutex支持可协商的撤回机制,正如在 ZK recipes 模块中描述的一样。
调用 makeRevocable 方法撤回互斥体

public void makeRevocable(RevocationListener<T> listener)
Make the lock revocable. Your listener will get called when another process/thread wants you to release the lock. Revocation is cooperative.
Parameters:
listener - the listener

使锁可撤销.当其他进程/线程想让你释放锁时,RevocationListener 监听器会被调用. 撤销是可协商的.
To ask for a lock to revoke/release, use the static method in the Revoker class:
让一个锁撤销/释放,可以使用 Revoker 类中的静态方法

public static void attemptRevoke(CuratorFramework client,
                                 String path)
                         throws Exception
Utility to mark a lock for revocation. Assuming that the lock has been registered
with a RevocationListener, it will get called and the lock should be released. Note,
however, that revocation is cooperative.
Parameters:
client - the client
path - the path of the lock - usually from something like InterProcessMutex.getParticipantNodes()

标记锁撤回的作用.如果锁已经注册了 RevocationListener 监听器,这个方法就会被调用,锁也会被释放. 注意:不管怎样,撤销都是可协商的。

5 错误处理

It is strongly recommended that you add a ConnectionStateListener and watch for SUSPENDED and LOST state changes. If a SUSPENDED state is reported you cannot be certain that you still hold the lock unless you subsequently receive a RECONNECTED state. If a LOST state is reported it is certain that you no longer hold the lock.

强烈推荐使用 ConnectionStateListener 监听器 监视 SUSPENDED and LOST 状态改变. 如果 SUSPENDED 状态被报告 ,你不能确信你依旧拥有这个锁(就是说不确定是不是还持有这把锁)除非你随后收到一个 RECONNECTED 状态. 如果 LOST 状态被报告 那可以确定你已经不再拥有这个锁了.

y
粉丝 5
博文 62
码字总数 9177
作品 0
南京
私信 提问
加载中
请先登录后再评论。
Swift百万线程攻破单例(Singleton)模式

一、不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法。单例的实现有多种方法,如下面: class SwiftSingleton { } 这段代码的实现,在shared中进行条...

一叶博客
2014/06/20
3.5K
16
PHP web 服务器--YACS

YACS 是一个强大的 PHP 脚本,可以让你维护一个动态的 Web 服务器。 特性: - Runs on your own server, or on a shared web site - Post articles with web forms, by e-mail, or remotely ......

匿名
2013/03/18
858
0
Apache Curator

zookeeper 的客户端调用过于复杂,Apache Curator 就是为了简化zookeeper客户端调用而生,利用它,可以更好的使用zookeeper。

李旸
2012/11/27
9.8K
0
TopoJSON

TopoJSON 是 GeoJSON 的扩展,增加了拓扑逻辑的编码。 Rather than representing geometries discretely, geometries in TopoJSON files are stitched together from shared line segments c......

匿名
2012/12/22
3.6K
0
Lisp的STM库--STMX

STMX 是一个高性能的 Common Lisp 库,用于实现可组合的软件事务内存机制。 事务内存是一种并行程序设计的方式,其来自于数据库管理系统(DBMS)中的事务(Transaction)概念。事务内存目前有...

匿名
2013/05/09
814
0

没有更多内容

加载失败,请刷新页面

加载更多

您如何加速Eclipse? - How can you speed up Eclipse?

问题: How can you make the experience with Eclipse faster? 如何使Eclipse体验更快? For instance: I disable all the plugins I don't need (Mylyn, Subclipse, …). 例如: 我禁用了所......

fyin1314
9分钟前
0
0
百度地图SDK新版内测邀请

本文作者:用****9 百度地图开放平台为开发者提供七大基础服务能力,其中地图SDK和导航SDK是开发者广泛使用的重要基础服务,为了满足开发者更多使用需求以及提升开发者集成后的应用效果,本次...

百度开发者中心
前天
21
0
获取JavaScript数组中的所有唯一值(删除重复项) - Get all unique values in a JavaScript array (remove duplicates)

问题: I have an array of numbers that I need to make sure are unique. 我需要确定一个唯一的数字数组。 I found the code snippet below on the internet and it works great until th......

javail
今天
11
0
如何检查字符串是否为空? - How to check if the string is empty?

问题: Does Python have something like an empty string variable where you can do: Python是否有类似空字符串变量的内容可以在其中执行: if myString == string.empty: Regardless, wh......

富含淀粉
今天
19
0
您如何存储未跟踪的文件? - How do you stash an untracked file?

问题: I have changes to a file, plus a new file, and would like to use git stash to put them away while I switch to another task. 我对一个文件进行了更改,再加上一个新文件,并希......

技术盛宴
今天
39
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部