您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# ReentrantLock怎么指定为公平锁
## 一、ReentrantLock简介
`ReentrantLock`是Java并发包(`java.util.concurrent.locks`)中提供的可重入互斥锁,它比`synchronized`关键字提供了更灵活的锁控制机制。主要特性包括:
- 可重入性:同一个线程可以多次获取同一把锁
- 可中断的锁获取
- 超时获取锁
- 公平性选择(本文重点)
## 二、公平锁与非公平锁的区别
### 1. 非公平锁(默认)
```java
// 默认构造方法创建的是非公平锁
ReentrantLock lock = new ReentrantLock();
// 通过构造参数指定为公平锁
ReentrantLock fairLock = new ReentrantLock(true);
// 传入true参数创建公平锁
ReentrantLock fairLock = new ReentrantLock(true);
查看ReentrantLock
构造方法:
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
公平锁通过FairSync
内部类实现,其核心逻辑在tryAcquire()
方法中:
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
// 关键区别:先检查是否有排队线程
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
// ...重入逻辑
}
public class FairLockExample {
private static final ReentrantLock fairLock = new ReentrantLock(true);
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
new Thread(() -> {
fairLock.lock();
try {
System.out.println(Thread.currentThread().getName() + "获取锁");
Thread.sleep(100);
} finally {
fairLock.unlock();
}
}, "Thread-" + i).start();
}
}
}
public class FairLockWithCondition {
private final ReentrantLock lock = new ReentrantLock(true);
private final Condition condition = lock.newCondition();
public void await() throws InterruptedException {
lock.lock();
try {
condition.await();
} finally {
lock.unlock();
}
}
public void signal() {
lock.lock();
try {
condition.signal();
} finally {
lock.unlock();
}
}
}
锁类型 | 吞吐量(ops/ms) | 平均延迟(ms) |
---|---|---|
非公平锁 | 15,632 | 0.12 |
公平锁 | 9,847 | 0.21 |
使用公平锁:
使用非公平锁:
不是完全公平的,因为: - 操作系统线程调度本身存在不确定性 - 等待队列中的线程被唤醒后仍需竞争CPU资源
ReentrantLock lock = new ReentrantLock(true);
System.out.println("是否是公平锁:" + lock.isFair());
不可以,必须在构造时确定,没有提供setter方法修改。
new ReentrantLock(true)
可创建公平锁合理选择锁的公平策略,可以在保证业务需求的同时获得最佳性能表现。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。