J.U.C并发框架之AQS(四):ReentrantLock公平锁

本篇继续之前的系列,解析ReentrantLock公平锁。

公平锁源码解析

之前的解析文章就已经提到,ReentrantLock默认创建的对象是非公平的:

1
2
3
public ReentrantLock() {
sync = new NonfairSync();
}

要创建公平锁需要自己指定:

1
2
3
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}

通过比较FairSync类和NonfairSync类,可知两者内部的tryAcquire方法在实现有所不同,具体到代码:

1
2
3
4
5
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}

实现公平锁的同步器FairSync会在尝试获得锁(tryAcquire方法)的过程中多出一行代码!hasQueuedPredecessors()。实现如下:

1
2
3
4
5
6
7
8
9
10
public final boolean hasQueuedPredecessors() {
// The correctness of this depends on head being initialized
// before tail and on head.next being accurate if the current
// thread is first in queue.
Node t = tail; // Read fields in reverse initialization order
Node h = head;
Node s;
return h != t &&
((s = h.next) == null || s.thread != Thread.currentThread());
}

在这个方法中,如果h == t,结合系列文章(一)的分析,要么之前还没有线程,要么前面只有一个线程,等待队列没有等待的线程,这时候直接返回false,当前线程尝试去获得锁;如果s != null且s.Thread是当前线程,那么当前线程就是head的后继节点,所以即便等待队列存在其它线程,也返回false并尝试去获取锁。

公平&非公平的具体体现

原理初探

公平锁和非公平锁在实现上基本相同,这就不得不让人产生困惑,锁的公平与不公体现在哪?现在假设有这样一个场景,开6个线程,当第1个启动的线程持有锁的时候,其余5个线程进入等待队列等待,等持有锁的线程释放锁的时候,接下来等待的线程拿锁的过程遵守公平&非公平原则吗??

看代码,这里创建一个非公平锁:

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
public class UnfairsyncDemo {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new ThreadDemoSync();
Thread t2 = new ThreadDemoSync();
Thread t3 = new ThreadDemoSync();
Thread t4 = new ThreadDemoSync();
Thread t5 = new ThreadDemoSync();
Thread t6 = new ThreadDemoSync();
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
}
public static class ThreadDemoSync extends Thread {
public static ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
lock.lock();
System.out.println(Thread.currentThread().getName());
try {
if (Thread.currentThread().getName().equals("Thread-0"))
Thread.sleep(3000); // 添加时间是为了确保所有的线程都已入列
System.out.println("线程" + Thread.currentThread() + "执行完sleep方法");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
}

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Thread-0
线程Thread[Thread-0,5,main]执行完sleep方法
Thread-1
线程Thread[Thread-1,5,main]执行完sleep方法
Thread-2
线程Thread[Thread-2,5,main]执行完sleep方法
Thread-3
线程Thread[Thread-3,5,main]执行完sleep方法
Thread-4
线程Thread[Thread-4,5,main]执行完sleep方法
Thread-5
线程Thread[Thread-5,5,main]执行完sleep方法
Process finished with exit code 0

多次测试都是相同的输出结果,不严谨的表明了线程是按照开启的顺序依次拿锁执行的。这不难通过分析来证明:当其它线程都进入等待队列等待的时候,唤醒是按进入队列的先后顺序依次由前置节点唤醒,这是由源码的设计决定的。所以无论是否设置公平锁,此时获得锁的顺序都是公平的。

接下来再构造一个简单的模型,思路是开启若干线程,每个线程在执行的过程中经过若干轮的尝试获得锁、释放锁,通过观察程序执行的结果来分析公平锁非公平锁的工作原理,这个例子引用自ReentrantLock(重入锁)以及公平性

构造观察模型

不妨设计5个线程,每个线程内循环30次,之所以设置30次是为了最后输出效果好一点点,线程内循环次数过少会因为程序运行快而使得等待队列没有等待的线程。

老规矩还是先放代码:

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
public class UnfairsyncDemo02 {
// private static Lock fairLock = new ReentrantLock02(true);
private static Lock unfairLock = new ReentrantLock02();
public static void main(String[] args) {
System.out.println("unfair version");
for (int i = 0; i < 5; i++) {
Thread thread = new Thread(new Job(unfairLock)){
@Override
public String toString() {
return getName();
}
};
thread.start();
}
}
private static class Job implements Runnable {
private Lock lock;
public Job(Lock lock) {
this.lock = lock;
}
@Override
public void run() {
for (int i = 0; i < 30; i++) {
lock.lock();
try {
System.out.println("Lock by: "
+ Thread.currentThread().getName() + " Waiting Threads: "
+ ((ReentrantLock02)lock).getQueuedThreads());
} finally {
lock.unlock();
}
}
}
}
private static class ReentrantLock02 extends ReentrantLock {
@Override
public Collection<Thread> getQueuedThreads() {
return super.getQueuedThreads();
}
}
}

简要说明一下这段代码,开5个线程,每个线程在执行“Job”任务时,会在run方法内循环30次,每1次都有一个获得锁释放锁的过程,并在run方法中打印当前持有锁的名称以及同步器内的等待队列的线程名。

这里再啰嗦一下:ReentrantLock内部拥有一个返回同步器等待队列的方法(返回的是一个list),访问受限(protected),所以在这里创建一个新的子类ReentrantLock02,目的就是方便程序调用getQueuedThreads方法。

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
unfair version
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-0 Waiting Threads: []
Lock by: Thread-2 Waiting Threads: [Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-1 Waiting Threads: [Thread-4, Thread-3, Thread-0]
Lock by: Thread-0 Waiting Threads: [Thread-4, Thread-3]
Lock by: Thread-0 Waiting Threads: [Thread-4, Thread-3]
Lock by: Thread-0 Waiting Threads: [Thread-4, Thread-3]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-3 Waiting Threads: [Thread-4]
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Lock by: Thread-4 Waiting Threads: []
Process finished with exit code 0

输出的每一行由相同的格式构成,比如这样一行:Lock by: Thread-2 Waiting Threads: [Thread-4, Thread-3, Thread-0, Thread-1],表示当前的锁被Thread-2持有,等待队列由前到后的顺序是:Thread-1、Thread-0、Thread-3、Thread-4。

结果其实已经体现了非公平锁的非公平性,每次新获得锁的线程,并不是等待队列里排最靠前的线程,这是因为上一个持有锁的线程释放锁的时候,如果有某个线程正好“乱入”,那么它就有可能在竞争胜利的情况下获得锁,而不会经过!hasQueuedPredecessors()的判断。

锁的公平和非公平这篇文章的最后提到了一个问题:为什么非公平锁下会出现线程连续获取锁的情况?实际上,由于本人将每个线程执行的循环设置为30次,输出结果已经表明线程并不是一直连续获取锁。而在循环次数少的情况下如果某个线程连续获取锁,我认为是因为线程的启动需要耗费资源和时间,而线程在由挂起到唤醒的过程中也会需要资源和时间,一个线程unlock()后也是优先执行tryAcquire方法,所以它比要被唤醒的线程更可能先获得锁。

将代码稍作修改就可以得到公平锁,由于前文已经将公平与否的原理分析清楚,这里不再赘述了,公平锁会按照等待队列的顺序依次获得锁,新近启动的线程也不能“插队”,例如(选取输出的一部分):

1
2
3
4
5
6
7
8
9
10
11
12
...
Lock by: Thread-0 Waiting Threads: [Thread-4, Thread-3, Thread-2, Thread-1]
Lock by: Thread-1 Waiting Threads: [Thread-0, Thread-4, Thread-3, Thread-2]
Lock by: Thread-2 Waiting Threads: [Thread-1, Thread-0, Thread-4, Thread-3]
Lock by: Thread-3 Waiting Threads: [Thread-2, Thread-1, Thread-0, Thread-4]
Lock by: Thread-4 Waiting Threads: [Thread-3, Thread-2, Thread-1, Thread-0]
Lock by: Thread-0 Waiting Threads: [Thread-4, Thread-3, Thread-2, Thread-1]
Lock by: Thread-1 Waiting Threads: [Thread-0, Thread-4, Thread-3, Thread-2]
Lock by: Thread-2 Waiting Threads: [Thread-1, Thread-0, Thread-4, Thread-3]
Lock by: Thread-3 Waiting Threads: [Thread-2, Thread-1, Thread-0, Thread-4]
Lock by: Thread-4 Waiting Threads: [Thread-3, Thread-2, Thread-1, Thread-0]
...

小结

公平锁虽然保证了所谓“公平”,但是线程状态的切换非常频繁,而且非公平锁等待队列中的线程被唤醒获得锁的过程也挺公平的,这就造成实际使用中通常采用非公平锁,但具体采用什么锁还是要结合具体的场景。

参考

ReentrantLock(重入锁)以及公平性:借用AQS内部的方法展现公平锁实现原理的例子;