ReentrantLock核心原理!

ReentrantLock是可重入的互斥锁。

ReentrantLock底层基于AbstractQueuedSynchronizer实现。

整体结构

ReentrantLock内部定义了专门的组件SyncSync继承AbstractQueuedSynchronizer提供释放资源的实现。

NonfairSyncFairSync是基于Sync扩展的子类,即ReentrantLock的非公平模式与公平模式。

它们作为Lock接口功能的基本实现。

image-20231017180401150

ReentrantLock中,它对AbstractQueuedSynchronizerstate状态值定义为线程获取该锁的重入次数。

state状态值为0表示当前没有被任何线程持有,state状态值为1表示被其他线程持有。

因为支持可重入,如果是持有锁的线程,再次获取同一把锁,直接成功,并且state状态值+1,线程释放锁state状态值-1

同理重入多次锁的线程,需要释放相应的次数。

Sync

Sync继承了AbstractQueuedSynchronizer,是ReentrantLock的核心,NonfairSyncFairSync都是基于Sync扩展出来的子类。

Sync实现了AQS类的释放资源(tryRelease),然后抽象了一个获取锁的函数(lock)让子类自行实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
abstract static class Sync extends AbstractQueuedSynchronizer {
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) { // state==0 代表资源可获取
//cas设置state为acquires,acquires传入的是1
if (compareAndSetState(0, acquires)) {
//cas成功,设置当前持有锁的线程
setExclusiveOwnerThread(current);
//返回成功
return true;
}
}
else if (current == getExclusiveOwnerThread()) { //如果state!=0,但是当前线程是持有锁线程,直接重入
//state状态+1
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
//设置state状态,此处不需要cas,因为持有锁的线程只有一个
setState(nextc);
//返回成功
return true;
}
//返回失败
return false;
}

/**
* 释放资源
*/
protected final boolean tryRelease(int releases) {
//state状态-releases,releases传入的是1
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread()) //如果当前线程不是持有锁线程,抛出异常
throw new IllegalMonitorStateException();
//设置返回状态,默认为失败
boolean free = false;
if (c == 0) {//state-1后,如果c==0代表释放资源成功
//返回状态设置为true
free = true;
//清空持有锁线程
setExclusiveOwnerThread(null);
}
//如果state-1后,state还是>0,代表当前线程有锁重入操作,需要做相应的释放次数,设置state值
setState(c);
return free;
}
}

tryRelease流程:

image-20231018135610476

NonfairSync

ReentrantLock中支持两种获取锁的策略,分别是非公平策略与公平策略,NonfairSync就是非公平策略。

NonfairSync继承Sync实现了lock函数,CAS设置状态值state1代表获取锁成功。

否则执行AQSacquire函数(获取锁模板)。

另外NonfairSync还实现了AQS留给子类实现的tryAcquire函数(获取资源)。

直接使用Sync提供的nonfairTryAcquire函数来实现tryAcquire

最后子类实现的tryAcquire函数在AQSacquire函数中被使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
static final class NonfairSync extends Sync {
private static final long serialVersionUID = 7316153563782823691L;

/**
* 获取锁
*/
final void lock() {
if (compareAndSetState(0, 1))//cas设置state为1成功,代表获取资源成功
//资源获取成功,设置当前线程为持有锁线程
setExclusiveOwnerThread(Thread.currentThread());
else
//cas设置state为1失败,代表获取资源失败,执行AQS获取锁模板流程,否获取资源成功
acquire(1);
}

/**
* 获取资源-使用的是Sync提供的nonfairTryAcquire函数
*/
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
}

/**
* AQS获取锁模板函数,这是AQS类中的函数
*/
public final void acquire(int arg) {
/**
* 我们只需要关注tryAcquire函数,后面的函数是AQS获取资源失败,线程节点进入CLH队列的细节流程,本文不关注
*/
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}

当前线程查看资源是否可获取:

可获取,尝试使用CAS设置state1CAS成功代表获取资源成功,否则获取资源失败。

不可获取,判断当线程是不是持有锁的线程,如果是,state重入计数,获取资源成功,否则获取资源失败。

image-20231017181634485

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 非公平-获取资源
*/
final boolean nonfairTryAcquire(int acquires) {
//获取当前线程
final Thread current = Thread.currentThread();
//获取当前状态
int c = getState();
if (c == 0) { // state==0 代表资源可获取
//cas设置state为acquires,acquires传入的是1
if (compareAndSetState(0, acquires)) {
//cas成功,设置当前持有锁的线程
setExclusiveOwnerThread(current);
//返回成功
return true;
}
}
//如果state!=0,但是当前线程是持有锁线程,直接重入
else if (current == getExclusiveOwnerThread()) {
//state状态+1
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
//设置state状态,此处不需要cas,因为持有锁的线程只有一个
setState(nextc);
//返回成功
return true;
}
//返回失败
return false;
}

FairSync

FairSync流程与NonfairSync基本一致,唯一的区别就是在CAS执行前,多了一步hasQueuedPredecessors函数,这一步就是判断当前线程是不是CLH队列被唤醒的线程,如果是就执行CAS,否则获取资源失败。

image-20231017181804586

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
static final class FairSync extends Sync {
private static final long serialVersionUID = -3000897897090466540L;

/**
* 获取锁
*/
final void lock() {
//cas设置state为1失败,代表获取资源失败,执行AQS获取锁模板流程,否获取资源成功
acquire(1);
}

/**
* 获取资源
*/
protected final boolean tryAcquire(int acquires) {
//获取当前线程
final Thread current = Thread.currentThread();
//获取state状态
int c = getState();
if (c == 0) { // state==0 代表资源可获取
//1.hasQueuedPredecessors判断当前线程是不是CLH队列被唤醒的线程,如果是执行下一个步骤
//2.cas设置state为acquires,acquires传入的是1
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
//cas成功,设置当前持有锁的线程
setExclusiveOwnerThread(current);
//返回成功
return true;
}
}
//如果state!=0,但是当前线程是持有锁线程,直接重入
else if (current == getExclusiveOwnerThread()) {
//state状态+1
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
//设置state状态,此处不需要cas,因为持有锁的线程只有一个
setState(nextc);
//返回成功
return true;
}
return false;
}
}

/**
* AQS获取锁模板函数,这是AQS类中的函数
*/
public final void acquire(int arg) {
/**
* 我们只需要关注tryAcquire函数,后面的函数是AQS获取资源失败,线程节点进入CLH队列的细节流程,本文不关注
*/
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}

Lock的实现

1
2
3
4
5
6
7
8
9
10
11
12
//同步器
private final Sync sync;

//默认使用非公平策略
public ReentrantLock() {
sync = new NonfairSync();
}

//true-公平策略 false非公平策略
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class ReentrantLock implements Lock, java.io.Serializable {
private static final long serialVersionUID = 7373984872572414699L;
//同步器
private final Sync sync;

//默认使用非公平策略
public ReentrantLock() {
sync = new NonfairSync();
}

//true-公平策略 false非公平策略
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}

/**
* 获取锁-阻塞
*/
public void lock() {
//基于sync实现
sync.lock();
}

/**
* 获取锁-阻塞,支持响应线程中断
*/
public void lockInterruptibly() throws InterruptedException {
//基于sync实现
sync.acquireInterruptibly(1);
}

/**
* 获取资源,返回是否成功状态-非阻塞
*/
public boolean tryLock() {
//基于sync实现
return sync.nonfairTryAcquire(1);
}

/**
* 获取锁-阻塞,支持超时
*/
public boolean tryLock(long timeout, TimeUnit unit)
throws InterruptedException {
//基于sync实现
return sync.tryAcquireNanos(1, unit.toNanos(timeout));
}

/**
* 释放锁
*/
public void unlock() {
//基于sync实现
sync.release(1);
}

/**
* 创建条件变量
*/
public Condition newCondition() {
//基于sync实现
return sync.newCondition();
}

}