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);
}
}