天天看點

curator 鎖之 Shared Reentrant Lock

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 狀态被報告 那可以确定你已經不再擁有這個鎖了.      

繼續閱讀