目录
lock()过程总结
通常用法如下:
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
lock.lock();
try {
//doSomething()
} finally {
lock.unlock();
}
}
lock()关键代码:
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
1)先去判断state
- tate=0:compareAndSetState(0,1)执行成功,state=state+1=1,return true 代表获锁所成功
- state>0:则判断是否是重入,是重入,则state=state+1 return true 代表重入成功
- 其他:retun false 代表获取锁失败。
2)获取锁失败后&#x