Skip to content

从ReentrantLock的实现看AQS的原理及应用中有一副以非公平锁方式加锁的图解误差? #1761

Description

@xiaopujun

问题描述:
1、并发编程--> 重要知识点-->从ReentrantLock的实现看AQS的原理及应用中有一副以非公平锁方式加锁的图解
2、其中如下部分的流程图是否与源码有出入呢?
image
3、源码jdk1.8如下

finalbooleannonfairTryAcquire(intacquires) {
finalThreadcurrent = Thread.currentThread();//获取当前线程intc = getState();
if (c == 0) {
if (compareAndSetState(0, acquires)) {//CAS抢锁setExclusiveOwnerThread(current);//设置当前线程为独占线程returntrue;//抢锁成功
}
}
elseif (current == getExclusiveOwnerThread()) {
intnextc = c + acquires;
if (nextc < 0) // overflowthrownewError("Maximum lock count exceeded");
setState(nextc);
returntrue;
}
returnfalse;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions