在底层分析中我们可知ReentrantLock实现Lock接口,在ReentrantLock中引用了AbstractQueuedSynchronizer的子类,所有的同步操作都是依靠AbstractQueuedSynchronizer(队列同步器)实现。
下面是Reetrantlock锁的方法调用图:
可以结合上图对底下代码做分析:
public class ReentrantLock implements Lock, java.io.Serializable { private static final long serialVersionUID = 7373984872572414699L; private final Sync sync; //AbstractQueuedSynchronizer 是一个抽象类,所以在使用这个同步器的时候,需要通过自己实现预期的逻辑,Sync、FairSync和NonfairSync都是ReentrantLock为了实现自己的需求而实现的内部类, //之所以做成内部类,我认为是只在ReentrantLock使用上述几个类,在外部没有使用到。 abstract static class Sync extends AbstractQueuedSynchronizer { //静态内部类Sync private static final long serialVersionUID = -5179523762034025860L; abstract void lock();//由于子类锁有公平和非公平之分,所以此方法需待子类来重写 final boolean nonfairTryAcquire(int acquires) {//父类中只实现了非公平性的获取锁 final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) // overflow throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } protected final boolean tryRelease(int releases) { int c = getState() - releases; if (Thread.currentThread() != getExclusiveOwnerThread()) throw new IllegalMonitorStateException(); boolean free = false; if (c == 0) { free = true; setExclusiveOwnerThread(null); } setState(c); return free; } protected final boolean isHeldExclusively() { return getExclusiveOwnerThread() == Thread.currentThread(); } final ConditionObject newCondition() { return new ConditionObject(); } ………………………………略 } *************************************************************************** static final class NonfairSync extends Sync {//静态内部类NonfairSync非公平锁 private static final long serialVersionUID = 7316153563782823691L; final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires);//直接调用父类的方法 } } ********************************************************************* static final class FairSync extends Sync {//静态内部类FairSync公平锁 private static final long serialVersionUID = -3000897897090466540L; final void lock() { acquire(1); } protected final boolean tryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } } ****************************************************************************************** public ReentrantLock() {//调用默认的构造函数返回来的是非公平锁 sync = new NonfairSync(); } public ReentrantLock(boolean fair) {//掉用带参的构造函数时,参数为true返回公平锁,参数为false返回非公平锁 sync = fair ? new FairSync() : new NonfairSync(); } //动态调用,由于在方法体内调用的是父类的抽象方法,但子类已重写了此方法,所以又动态的调用子类重写后的方法 public void lock() { sync.lock(); } public boolean tryLock() { return sync.nonfairTryAcquire(1); } public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireNanos(1, unit.toNanos(timeout)); } public void unlock() { sync.release(1); } public Condition newCondition() { return sync.newCondition(); } } //补充方法 //从上面的代码中,我们可以观察到不论是公平还是非公平锁,在其方法体内最终都会调用acquire方法,该方法是继承的AQS机制的。 public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }