之前已经搬运过lock锁的获取,现在看一下lock锁的锁释放方法(unlock)的源码,正如unlock的语义一样,unlock方法是Lock接口中定义的解锁方法,释放线程获取的资源,并通知等待线程获取资源。
/**
* Attempts to release this lock.
*
* <p>If the current thread is the holder of this lock then the hold
* count is decremented. If the hold count is now zero then the lock
* is released. If the current thread is not the holder of this
* lock then {@link IllegalMonitorStateException} is thrown.
*
* @throws IllegalMonitorStateException if the current thread does not
* hold this lock
*/
public void unlock() {
sync.release(1);
}
在实现类ReentrantLock中,unlock方法很简单,就是调用了一下抽象内部类Sync中的release,可以说真正处理释放功能的正是release方法。
/**
* Releases in exclusive mode. Implemented by unblocking one or
* more threads if {@link #tryRelease} returns true.
* This method can be used to implement method {@link Lock#un