小编给大家分享一下ReentrantLock的实现原理是什么,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
在并发编程中,除了synchronized关键字,java并发包中java.util.concurrent.locks中的ReentrantLock和ReentrantReadWriteLock也是常用的锁实现。本篇从源码方面,分析一下重入锁ReentrantLock的原理。
先说一下什么的重入锁:某个线程获得锁以后,还可以多次重复获得锁,不会自己阻塞自己。
ReentrantLock基于抽象类AbstractQueuedSynchronizer(以下简称AQS)实现。
看源码:
首先从构造器上可以看出,ReentrantLock有公平锁和非公平锁两种机制。
//默认非公平锁
public ReentrantLock() {
sync = new NonfairSync();
}
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
先简要说明一下公平锁和非公平锁的区别,然后在分析两者的不同实现方式。
公平锁:多个线程之间讲究先来后到。类似于排队,后面来的线程依次排在队列最后。
非公平锁:进行锁的争抢。抢到就执行,没抢到就阻塞。等待获得锁的线程释放后,再参与竞争。
所以通常使用非公平锁。其效率比公平锁高。
获取锁
公平锁
final void lock() {
acquire(1);
}
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
第一步tryAcquire(arg)尝试加锁,由FairSync实现,具体代码如下:
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;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
获取当前线程
获取AQS中的state。如果state为0,表示此时没有线程获得锁。
在if判断中,先要判断AQS的Node队列是否为空。如果不是空的,就需要排队。此时不获取锁。
尝试使用CAS算法,将state更新为1。更新成功,获取锁,将此时的线程设置为独占线程exclusiveOwnerThread。返回true。
如果state不为0,表示已经有线程获得了锁。所以要判断获得锁的线程(独占线程)是否为当前线程。
如果是,说明是重入情况。将state增加1。返回true。
走到最后一步,就是没有获得锁了。返回false;
继续上面的步骤,如果获取锁失败,先执行addWaiter(Node.EXCLUSIVE),将当前线程写入队列
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
enq(node);
return node;
}
封装一个新节点node
判断链表尾是否为空,不是就把新节点node‘写入最后
’链表尾为空,则用enq(node)写入最后。
写入队列以后,acquireQueued()方法,挂起当前线程。
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
在循环中,如果node的上一个是头节点,则再尝试获取锁。成功就结束循环,返回false
不是头节点,就根据上一个节点的waitStatus,判断是否需要挂起当前线程。waitStatus用来记录节点状态,如节点取消,节点等待等。
判断需要挂起,则使用parkAndCheckInterrupt()方法,挂起线程。具体使用LockSupport.park(this)挂起线程。
如果在这里的第一步就获取锁成功了,就可以取消此节点的获取锁操作了。
非公平锁
非公平锁在锁的获取策略上有差异。
final void lock() {
if (compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
acquire(1);
}
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
非公平锁先直接尝试使用CAS算法更新state,获取锁
更新失败以后,在尝试获取锁
final boolean nonfairTryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
与公平锁相比,非公平锁尝试获取锁的过程中,无需判断队列中是否存在其他线程。
释放锁
公平锁和非公平锁释放锁的步骤都一样
public void unlock() {
sync.release(1);
}
public final boolean release(int arg) {
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
unparkSuccessor(h);
return true;
}
return false;
}
//更新state
protected final boolean tryRelease(int releases) {
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
boolean free = false;
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
值得注意的是,因为是重入锁的关系,在tryRelease()方法中,需要将state更新为0,才认为完全释放锁。释放以后,再唤醒挂起线程。
以上是ReentrantLock的实现原理是什么的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。