ReentrantLock
类是属于java.util.concurrent
的。实现了Lock, java.io.Serializable
两个接口,是一个可重入的互斥锁,所谓可重入是线程能够重复获取已经持有的锁。java
/** * Creates an instance of {@code ReentrantLock}. * This is equivalent to using {@code ReentrantLock(false)}. */ public ReentrantLock() { sync = new NonfairSync(); } /** * Creates an instance of {@code ReentrantLock} with the * given fairness policy. * * @param fair {@code true} if this lock should use a fair ordering policy */ public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }
abstract static class Sync extends AbstractQueuedSynchronizer
ReentrantLock
实现锁的机制是经过Sync
进行操做的。Sync
类是继承AbstractQueuedSynchronizer
类的。这也就代表ReentrantLock
是基于AQS
的实现的。‘node
Sync
,FairSync
和NonFairSync
都是ReentrantLock
的静态内部类。FairSync
和NonFairSync
又是Sync
具体实现类,分别对应的是公平锁和非公平锁,公平主要是指按照FIFO
原则顺序获取锁,非公平能够根据定义的规则来选择得到锁。less
非公平锁NonFairSync
是ReentrantLock
默认的实现方式。这里能够看一下它的lock
实现过程:源码分析
/** * Performs lock. Try immediate barge, backing up to normal * acquire on failure. */ final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); }
CAS
更新state
状态,若是更新成功,则获取锁,设定当前线程为锁的拥有者。protected final boolean compareAndSetState(int expect, int update) { // See below for intrinsics setup to support this return unsafe.compareAndSwapInt(this, stateOffset, expect, update); }
acquire(1)
方法。acquire
具体实现以下:public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); }
tryAcquire
过程,将再次尝试获取锁,其中tryAcquire
在静态内部类NonfairSync
类中被重写,具体的实现是Sync
的nonfairTryAcquire
方法:ui
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; }
其主要过程是先获取state
的值,若是等于0,则经过CAS
更新state
的值。若是state
不为0,则判断当前线程是不是锁的持有者,若是是,则将state
加1,返回true
。this
若是tryAcquire
仍然失败的话,首先会调用addWaiter(Node.EXCLUSIVE)
,将当前线程加入到等待队列的尾部。而后会调用acquireQueued
方法,acquireQueued
的做用主要是用来阻塞线程的:spa
/** * Acquires in exclusive uninterruptible mode for thread already in * queue. Used by condition wait methods as well as acquire. * * @param node the node * @param arg the acquire argument * @return {@code true} if interrupted while waiting */ final boolean acquireQueued(final Node node, int arg) { boolean failed = true; try { boolean interrupted = false; for (;;) { final Node p = node.predecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GC failed = false; return interrupted; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } finally { if (failed) cancelAcquire(node); } }
这里是一个循环自旋操做,在阻塞线程以前,首先判断线程自身前面的节点是不是head
节点,若是是,则从新去获取锁,获取成功后,返回,并取消不断获取的过程。若是不是,调用shouldParkAfterFailedAcquire
方法去判断是否应该阻塞当前线程,主要是经过节点的waitStatus
来进行判断。线程
公平锁FairSync
和非公平锁NonFairSync
的实现很类似,这里比较一下二者的差异。code
FairSync
的lock
方法中没有像NonFairSync
中先去经过CAS
操做state
去获取锁,而是直接经过tryAcquire
去获取锁。final void lock() { acquire(1); }
FairSync
版本tryAcquire
在获取锁的过程当中,须要先判断队列中是否有其余等待的线程,若是没有,才回去尝试获取锁。/** * Fair version of tryAcquire. Don't grant access unless * recursive call or no waiters or is first. */ 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; }
释放锁没有区分公平和非公平的。主要的工做就是减少state
的值。当state
等0的时候,释放锁并唤醒队里中其余线程来获取锁。orm
public void unlock() { sync.release(1); }
public final boolean release(int arg) { if (tryRelease(arg)) { Node h = head; if (h != null && h.waitStatus != 0) unparkSuccessor(h); 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; }
ReentrantLock
是经过AQS
的state
字段来判断所是否被占用。公平
与非公平
的差异是在于获取锁的方式是不是按照顺序的。state
操做是经过CAS
实现的。经过队列来实现因抢占锁被阻塞的队列。AQS
有自旋的过程,并不是是获取不到锁就直接阻塞。