一、描述
共享鎖:全局同步分佈式鎖,同一時間兩臺機器只能有一臺得到鎖less
二、參與類
InterProcessMutex分佈式
三、用法oop
3.1 建立ui
InterProcessMutex 實例this
public InterProcessMutex(CuratorFramework client, String path) Parameters: client - client path - the path to lock
3.2 通常用法線程
用其中一個方法,獲取鎖code
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() 平衡。orm
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 實例 是可重用的.不用每次都建立一個新的實例. 重用一個單實例.ip
四、撤銷
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 狀態被報告 那能夠肯定你已經再也不擁有這個鎖了.