Skip to content

Commit 9ee40d3

Browse files
committed
增加注释
1 parent d59ad4c commit 9ee40d3

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

src/jdk/java.base/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,13 +1187,13 @@ private long enableWait(ConditionNode node) {
11871187
node.waiter = Thread.currentThread();
11881188
node.setStatusRelaxed(COND | WAITING);
11891189
ConditionNode last = lastWaiter;
1190-
if (last == null)
1190+
if (last == null) // 单链表入队
11911191
firstWaiter = node;
11921192
else
11931193
last.nextWaiter = node;
11941194
lastWaiter = node;
11951195
long savedState = getState();
1196-
if (release(savedState))
1196+
if (release(savedState)) // 释放锁。只有释放锁,其他的线程才能获取锁来执行程序
11971197
return savedState;
11981198
}
11991199
node.status = CANCELLED; // lock not held or inconsistent
@@ -1313,12 +1313,12 @@ else if ((node.status & COND) != 0) {
13131313
* </ol>
13141314
*/
13151315
public final void await() throws InterruptedException {
1316-
if (Thread.interrupted())
1316+
if (Thread.interrupted()) // 收到中断信号,抛出异常
13171317
throw new InterruptedException();
13181318
ConditionNode node = newConditionNode();
13191319
if (node == null)
13201320
return;
1321-
long savedState = enableWait(node);
1321+
long savedState = enableWait(node); // 建立 Condition 链表,释放锁
13221322
LockSupport.setCurrentBlocker(this); // for back-compatibility
13231323
boolean interrupted = false, cancelled = false, rejected = false;
13241324
while (!canReacquire(node)) {

src/jdk/java.base/java/util/concurrent/locks/AbstractQueuedSynchronizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ final int acquire(Node node, int arg, boolean shared,
739739
} else if (pred == null) { // try to enqueue
740740
node.waiter = current;
741741
node.setPrevRelaxed(t); // avoid unnecessary fence
742-
if (!casTail(t, node))
742+
if (!casTail(t, node)) 入队
743743
node.setPrevRelaxed(null); // back out
744744
else
745745
t.next = node;

src/jdk/java.base/java/util/concurrent/locks/ReentrantLock.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ static final class NonfairSync extends Sync {
222222

223223
final boolean initialTryLock() {
224224
Thread current = Thread.currentThread();
225+
// 非公平锁与公平锁对比一下:非公平锁,先抢锁,不考虑排队
225226
if (compareAndSetState(0, 1)) { // first attempt is unguarded
226227
setExclusiveOwnerThread(current); // 上来就抢,不考虑是否有排队,这是非公平锁
227228
return true;
228-
} else if (getExclusiveOwnerThread() == current) {
229+
} else if (getExclusiveOwnerThread() == current) { // 判断是否是重入
229230
int c = getState() + 1;
230231
if (c < 0) // overflow
231232
throw new Error("Maximum lock count exceeded");
@@ -259,6 +260,7 @@ static final class FairSync extends Sync {
259260
final boolean initialTryLock() {
260261
Thread current = Thread.currentThread();
261262
int c = getState();
263+
// 非公平锁与公平锁对比一下:锁没有被抢,等待队列中前面没有排队节点才去抢锁
262264
if (c == 0) { // 锁没有被抢,且没有排队线程才去抢锁,这是公平锁。
263265
if (!hasQueuedThreads() && compareAndSetState(0, 1)) {
264266
setExclusiveOwnerThread(current);

src/jdk/java.base/java/util/concurrent/locks/ReentrantReadWriteLock.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,10 @@ protected final boolean tryAcquire(int acquires) {
401401
int w = exclusiveCount(c);
402402
if (c != 0) {
403403
// (Note: if c != 0 and w == 0 then shared count != 0)
404+
// ❓w==0 为什么就有读锁?答:因为前面还有 c != 0 的条件
404405
if (w == 0 || current != getExclusiveOwnerThread())
405406
return false;
407+
// 因为 c != 0 & w != 0,则必然是写锁持有锁
406408
if (w + exclusiveCount(acquires) > MAX_COUNT)
407409
throw new Error("Maximum lock count exceeded");
408410
// Reentrant acquire
@@ -480,15 +482,16 @@ protected final int tryAcquireShared(int unused) {
480482
getExclusiveOwnerThread() != current)
481483
return -1;
482484
int r = sharedCount(c);
483-
if (!readerShouldBlock() && // 读锁不被阻塞
485+
if (!readerShouldBlock() && // 读锁不被阻塞:非公平锁是第一个节点是抢写锁的线程;公平锁,前面有排队就阻塞。
484486
r < MAX_COUNT && // 且并发线程数少于最大阈值
485-
compareAndSetState(c, c + SHARED_UNIT)) { // 抢锁成功
487+
compareAndSetState(c, c + SHARED_UNIT)) { // 抢锁:高16位。JDK25中已经变成高32位了。
486488
if (r == 0) { // 第一个拿到读锁的线程
487489
firstReader = current;
488490
firstReaderHoldCount = 1;
489491
} else if (firstReader == current) {
490492
firstReaderHoldCount++;
491493
} else {
494+
// 统计变量,对流程没影响
492495
HoldCounter rh = cachedHoldCounter;
493496
if (rh == null ||
494497
rh.tid != LockSupport.getThreadId(current))

src/jdk/java.base/java/util/concurrent/locks/StampedLock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
* @jls 17.4 Memory Model
239239
* @since 1.8
240240
* @author Doug Lea
241-
*/
241+
*/ // TODO ❓读写不互斥,是不是比 ReentrantLock 更适合 BlockingQueue?
242242
public class StampedLock implements java.io.Serializable { // 读写也不互斥
243243
/*
244244
* Algorithmic notes:

0 commit comments

Comments
 (0)