深入理解独占锁ReentrantLock类锁( 三 )

【2】抽象的Sync类部分
abstract static class Sync extends AbstractQueuedSynchronizer {//定义了核心的加锁逻辑@ReservedStackAccessfinal boolean tryLock() {Thread current = Thread.currentThread();//获取State属性值,这是在AQS里面定义的值,用于标记是否可以加锁,0代表没有人在用锁,1代表有人在占用,大于1说明这个锁被这个人加了多次【即重入锁概念】int c = getState();if (c == 0) {//CAS保证只有一个人能成功if (compareAndSetState(0, 1)) {//设置持有锁的线程setExclusiveOwnerThread(current);return true;}} else if (getExclusiveOwnerThread() == current) { //走到这里说明有人持有了锁,但是可以判断持有的人是不是自己【可重入】if (++c < 0) // overflowthrow new Error("Maximum lock count exceeded");//因为每一次重入都会导致State的值+1,所以解锁的时候对应要减1setState(c);return true;}return false;}//为子类留下的加锁逻辑的抽象方法abstract boolean initialTryLock();//核心加锁逻辑里面便是使用抽象方法进行加锁@ReservedStackAccessfinal void lock() {if (!initialTryLock())acquire(1);}@ReservedStackAccessfinal void lockInterruptibly() throws InterruptedException {if (Thread.interrupted())throw new InterruptedException();if (!initialTryLock())acquireInterruptibly(1);}@ReservedStackAccessfinal boolean tryLockNanos(long nanos) throws InterruptedException {if (Thread.interrupted())throw new InterruptedException();return initialTryLock() || tryAcquireNanos(1, nanos);}//尝试释放锁@ReservedStackAccessprotected final boolean tryRelease(int releases) {int c = getState() - releases;if (getExclusiveOwnerThread() != Thread.currentThread())throw new IllegalMonitorStateException();boolean free = (c == 0);if (free)setExclusiveOwnerThread(null);setState(c);return free;}protected final boolean isHeldExclusively() {// While we must in general read state before owner,// we don't need to do so to check if current thread is ownerreturn getExclusiveOwnerThread() == Thread.currentThread();}final ConditionObject newCondition() {return new ConditionObject();}// Methods relayed from outer classfinal Thread getOwner() {return getState() == 0 ? null : getExclusiveOwnerThread();}final int getHoldCount() {return isHeldExclusively() ? getState() : 0;}final boolean isLocked() {return getState() != 0;}/*** Reconstitutes the instance from a stream (that is, deserializes it).*/private void readObject(java.io.ObjectInputStream s)throws java.io.IOException, ClassNotFoundException {s.defaultReadObject();setState(0); // reset to unlocked state}}【3】实现抽象的 Sync类 的公平锁 FairSync类部分
static final class FairSync extends Sync {//尝试锁定方法final boolean initialTryLock() {Thread current = Thread.currentThread();int c = getState();if (c == 0) {//看得出来首先队列要为空,其次才是CAS加锁成功,才算能够持有锁//也就是说队列不为空,连CAS加锁的资格都没有,所以十分公平if (!hasQueuedThreads() && compareAndSetState(0, 1)) {setExclusiveOwnerThread(current);return true;}} else if (getExclusiveOwnerThread() == current) {if (++c < 0) // overflowthrow new Error("Maximum lock count exceeded");setState(c);return true;}return false;}//尝试获取方法protected final boolean tryAcquire(int acquires) {if (getState() == 0 && !hasQueuedPredecessors() &&compareAndSetState(0, acquires)) {setExclusiveOwnerThread(Thread.currentThread());return true;}return false;}}//AbstractQueuedSynchronizer类#hasQueuedThreads方法//判断队列是否为空【由于AQS里面采用的是链表实现队列效果,所以是判断节点情况】public final boolean hasQueuedThreads() {for (Node p = tail, h = head; p != h && p != null; p = p.prev)if (p.status >= 0)return true;return false;}【4】实现抽象的 Sync类 的非公平锁 NonfairSync类部分
//与公平的同步器进行比较的话,会发现,他们本质没什么区别,因为大多数走的都是抽象方法的逻辑和AQS的方法//最大的区别在于加锁的方式不同,公平方式,队列没人才去加锁;非公平方式,不管队列有没有人,都是直接去加锁,加到了就持有static final class NonfairSync extends Sync {//尝试锁定方法final boolean initialTryLock() {Thread current = Thread.currentThread();//直接尝试CAS获取加锁权利if (compareAndSetState(0, 1)) { // first attempt is unguardedsetExclusiveOwnerThread(current);return true;} else if (getExclusiveOwnerThread() == current) {int c = getState() + 1;if (c < 0) // overflowthrow new Error("Maximum lock count exceeded");setState(c);return true;} elsereturn false;}//尝试获取方法protected final boolean tryAcquire(int acquires) {//判断是否有人持有锁,没有则去加锁if (getState() == 0 && compareAndSetState(0, acquires)) {setExclusiveOwnerThread(Thread.currentThread());return true;}return false;}}

经验总结扩展阅读