释放锁的逻辑-InterProcessMutex.release
生活随笔
收集整理的這篇文章主要介紹了
释放锁的逻辑-InterProcessMutex.release
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public void release() throws Exception{ Thread currentThread = Thread.currentThread(); LockData lockData = threadData.get(currentThread); if ( lockData == null ){ // 無法從映射表中獲取鎖信息,不持有鎖 throw new IllegalMonitorStateException("You do not own the lock: " + basePath); } int newLockCount = lockData.lockCount.decrementAndGet(); if ( newLockCount > 0 ){ // 鎖是可重入的,初始值為1,原子-1到0,鎖才釋放 return; } if ( newLockCount < 0 ){ // 理論上無法執行該路徑 throw new IllegalMonitorStateException("Lock count has gone negative for lock: " + basePath); } try{ // lockData != null && newLockCount == 0,釋放鎖資源 internals.releaseLock(lockData.lockPath); } finally { // 最后從映射表中移除當前線程的鎖信息 threadData.remove(currentThread); }
}
?
總結
以上是生活随笔為你收集整理的释放锁的逻辑-InterProcessMutex.release的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Curator实现分布式锁的基本原理-g
- 下一篇: 释放锁的逻辑-LockInternals