ReentrantLock公平锁与非公平锁的lock()和unlock()过程解析. 源码比较简单, 静下心来就可以看懂, 本文旨在记录加锁与解锁的过程.
ReentrantLock.lock()
public void lock() {
this.sync.lock();
}
sync.lock()取决于ReentrantLock获取锁的机制: 公平性FairSync or 非公平性NonfairSync(Default)
ReentrantLock自定义同步器Sync
FairSync, NonfairSync
abstract static class Sync extends AbstractQueuedSynchronizer
static final class FairSync extends ReentrantLock.Sync
static final class NonfairSync extends ReentrantLock.Sync
非公平性(实现重进入的过程)
1. ReentrantLock默认是NonfairSync
public class ReentrantLock implements Lock, Serializable {
private final ReentrantLock.Sync sync; // 自定义同步器
public ReentrantLock() {
// 默认是NonfairSync
this.sync = new ReentrantLock.NonfairSync();
}
2.NonfairSync获取锁调用过程:
ReentrantLock.lock() –> this.sync.lock();(即 NonfairSync.lock()) –> AbstractQueueSynchronizer.acquire() –> NonfairSync.tryAcquire(int var)
// NonfairSync.NonfairSync
final void lock() {
if(this.compareAndSetState(0, 1)) {
// 当前无线程占用同步锁
this.setExclusiveOwnerThread(Thread.currentThread());
} else {
this.acquire(1);
}
}
// AbstractQueueSynchronizer.acquire()
public final void acquire(int var1) {
if(!this.tryAcquire(var1) && this.acquireQueued(this.addWaiter(AbstractQueuedSynchronizer.Node.EXCLUSIVE), var1)) {
selfInterrupt();
}
}
// // NonfairSync.NonfairSync
protected final boolean tryAcquire(int var1) {
return this.nonfairTryAcquire(var1);
}
ReentrantLock自定义同步器Sync.nonfairTryAcquire(int var)
该方法增加了再次获取同步状态的处理逻辑
final boolean nonfairTryAcquire(int var1) {
Thread var2 = Thread.currentThread();
int var3 = this.getState();
if(var3 == 0) { // 首次获取同步锁
if(this.compareAndSetState(0, var1)) {
this.setExclusiveOwnerThread(var2);
return true;
}
} else if(var2 == this.getExclusiveOwnerThread()) {
// 当前线程为获取锁的线程, 增加同步状态值
int var4 = var3 + var1;
if(var4 < 0) {
throw new Error("Maximum lock count exceeded");
}
this.setState(var4);
return true;
}
// 不是以上两种情况,则等待获取同步锁
return false;
}
3. ReentrantLock释放锁调用全过程:
ReentrantLock.unlock() –> this.sync.release(1); (即 AbstractQueueSynchronizer.released()) –> ReentrantLock.Sync.tryRelease()
// ReentrantLock
public void unlock() {
this.sync.release(1);
}
// AbstractQueueSynchronizer
public final boolean release(int var1) {
if(this.tryRelease(var1)) {
AbstractQueuedSynchronizer.Node var2 = this.head;
if(var2 != null && var2.waitStatus != 0) {
this.unparkSuccessor(var2); // 唤醒头节点的后继节点
}
return true;
} else {
return false;
}
}
ReentrantLock自定义同步器Sync.tryRelease(int var)
倘若该锁被获取了n次
protected final boolean tryRelease(int var1) {
int var2 = this.getState() - var1;
if(Thread.currentThread() != this.getExclusiveOwnerThread()) {
throw new IllegalMonitorStateException();
} else {
// 前n-1次返回为false
boolean var3 = false;
if(var2 == 0) {
// 只有该线程的同步状态完全释放,才返回false,占有线程为null。
var3 = true;
this.setExclusiveOwnerThread((Thread)null);
}
this.setState(var2);
return var3;
}
}
公平性 (FIFO)
1. ReentrantLock构造函数
public ReentrantLock(boolean var1) {
// True为公平锁, False为非公平锁
this.sync = (ReentrantLock.Sync)(var1?new ReentrantLock.FairSync():new ReentrantLock.NonfairSync());
}
2. FairSync获取锁全过程
ReentrantLock.lock() –> this.sync.lock();(即 FairSync.lock()) –> AbstractQueueSynchronizer.acquire() –> FairSync.tryAcquire(int var)
// ReentrantLock.FairSync
final void lock() {
this.acquire(1);
}
// AbstractQueueSynchronizer
public final void acquire(int var1) {
if(!this.tryAcquire(var1) && this.acquireQueued(this.addWaiter(AbstractQueuedSynchronizer.Node.EXCLUSIVE), var1)) {
selfInterrupt();
}
}
// ReentrantLock.FairSync
protected final boolean tryAcquire(int var1) {
Thread var2 = Thread.currentThread();
int var3 = this.getState();
if(var3 == 0) {
if(!this.hasQueuedPredecessors() && this.compareAndSetState(0, var1)) {
this.setExclusiveOwnerThread(var2);
return true;
}
} else if(var2 == this.getExclusiveOwnerThread()) {
int var4 = var3 + var1;
if(var4 < 0) {
throw new Error("Maximum lock count exceeded");
}
this.setState(var4);
return true;
}
return false;
}
与ReentrantLock.UnfairSync.nonfairTryAcquire()相比较, 判断条件多了!this.hasQueuedPredecessors()
(同步队列中当前节点是否有前驱节点, true为有线程比当前线程更早地请求获取锁, ==需要等待前驱线程获取并释放锁之后才能继续获取锁==)