Last updated on 2025-11-23T20:37:01+08:00
1. TODO
2. 脑图
Xmind
Edraw
Hexo 地址
👉 http://blog.wangjia.ink/2025/08/31/源码:java.util.concurrent.locks.ReentrantLock源码解析/
3. 基础部分
3.1. ReentrantLock 概述
ReentrantLock 是一个具体类,实现了 java.util.concurrent.locks.Lock、java.io.Serializable

ReentrantLock 是一种可重入、悲观、公平或非公平、互斥的锁,并且是使用 AQS 实现的独占模式的同步器。State 和共享资源表示以下含义:
State 表示锁的重入次数
0
- 表示锁空闲,线程可以尝试获取
1
- 表示锁已被某个线程持有
n(n > 1)
- 表示同一线程对该锁进行了
n 次重入
- 共享资源表示加锁期间的操作
ReentrantLock 功能类似于 synchronized,但更灵活、功能更强大,主要包括:
ReentrantLock 支持阻塞竞争锁、限时阻塞竞争锁、非阻塞竞争锁,而 synchronized 只支持阻塞竞争锁
ReentrantLock 在阻塞竞争锁、限时阻塞竞争锁时能响应中断,而 synchronized 在阻塞竞争锁时不能响应中断
ReentrantLock 既可以是公平锁,也可以是非公平锁,而 synchronized 只能是非公平锁
ReentrantLock 支持多条条件队列,而 synchronized 只支持一条条件队列(即 waitSet,因为在语义上,EntryList 不属于条件队列)
[!NOTE] 注意事项
- 详见源码:
Lock
obsidian 内部链接:
- 源码:java.util.concurrent.locks.Lock源码解析
Hexo 链接:
- http://blog.wangjia.ink/2025/11/07/源码:java.util.concurrent.locks.Lock源码解析/
- 详见源码:
Serializable
obsidian 内部链接:
- 源码:java.io.Serializable源码解析
Hexo 链接:
- http://blog.wangjia.ink/2025/10/28/源码:java.io.Serializable源码解析/
- 锁竞争激烈的场景,建议优先使用
ReentrantLock,因为它没有 “锁升级” 的额外开销:
synchronized 在偏向锁、轻量级锁时,效率的确很高
- 但是,一旦升级为重量级锁,就无法降级,而且在锁竞争激烈的场景下性能会受到影响
- 如果你对并发机制掌握不深,建议优先使用
synchronized,因为它语法简洁、出错率低。但如果你需要更强的灵活性,并且熟悉并发编程,ReentrantLock 会更合适
4. 内部类
4.1. Sync
详见源码:ReentrantLock.Sync
obsidian 内部链接:
- 源码:java.util.concurrent.locks.ReentrantLock.Sync源码解析
Hexo 链接:
- http://blog.wangjia.ink/2025/09/02/源码:java.util.concurrent.locks.ReentrantLock.Sync源码解析/
4.2. NonfairSync
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
| static final class NonfairSync extends Sync { final boolean initialTryLock() { Thread current = Thread.currentThread(); if (compareAndSetState(0, 1)) { setExclusiveOwnerThread(current); return true; } else if (getExclusiveOwnerThread() == current) { int c = getState() + 1; if (c < 0) throw new Error("Maximum lock count exceeded"); setState(c); return true; } else return false; } protected final boolean tryAcquire(int acquires) { if (getState() == 0 && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(Thread.currentThread()); return true; } return false; } }
|
4.3. FairSync
FairSync 用于实现公平锁的加锁策略。
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
| static final class FairSync extends Sync { final boolean initialTryLock() { Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (!hasQueuedThreads() && compareAndSetState(0, 1)) { setExclusiveOwnerThread(current); return true; } } else if (getExclusiveOwnerThread() == current) { if (++c < 0) throw new Error("Maximum lock count exceeded"); setState(c); return true; } return false; } protected final boolean tryAcquire(int acquires) { if (getState() == 0 && !hasQueuedPredecessors() && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(Thread.currentThread()); return true; } return false; } }
|
5. 构造方法
5.1. ReentrantLock()
该方法用于构造一个非公平锁的 `ReentrantLock
1 2 3 4 5
| public ReentrantLock() { sync = new NonfairSync(); }
|
5.2. ReentrantLock(boolean fair)
该方法构造一个公平锁或非公平锁的 ReentrantLock
1 2 3 4 5
| public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }
|
6. 实例方法
6.1. 实例具体方法
6.1.1. 具体方法(实现)
6.1.1.1. Lock 中接口方法的实现
6.1.1.1.1. void lock()
1 2 3 4 5
| public void lock() { sync.lock(); }
|
6.1.1.1.2. void lockInterruptibly()
1 2 3 4 5
| public void lockInterruptibly() throws InterruptedException { sync.lockInterruptibly(); }
|
6.1.1.1.3. boolean tryLock()
1 2 3 4 5
| public boolean tryLock() { return sync.tryLock(); }
|
6.1.1.1.4. boolean tryLock(long timeout, TimeUnit unit)
1 2 3 4 5 6
| public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryLockNanos(unit.toNanos(timeout)); }
|
6.1.1.1.5. void unlock()
1 2 3 4 5
| public void unlock() {
sync.release(1); }
|
6.1.1.1.6. Condition newCondition()
1 2 3 4 5
| public Condition newCondition() { return sync.newCondition(); }
|