???????Java??????????
???????????? ???????[ 2013/3/20 10:13:40 ] ????????
???????4?????????????????????÷??????????
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState(); //????????????????volatile????state
if (c == 0) {
if (isFirst(current) &&
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;
}
??????????????????????????????????????????volatile????state??
????????ù???????????????unlock()????????ù????£?
????1??ReentrantLock:unlock()
????2??AbstractQueuedSynchronizer:release(int arg)
????3??Sync:tryRelease(int releases)
???????3??????????????????????÷??????????
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); //????????дvolatile????state
return free;
}
??????????????????????????????????????дvolatile????state??
???????????????????дvolatile????state?????????????????volatile??????????volatile??happens-before????????????????дvolatile????????????????????????????????????volatile???????????????????????????
???????????????????????????????????
???????????????????????????????????????????????????????
??????ù???????????????lock()????????ù????£?
????1??ReentrantLock:lock()
????2??NonfairSync:lock()
????3??AbstractQueuedSynchronizer:compareAndSetState(int expect?? int update)
???????3?????????????????????÷??????????
protected final boolean compareAndSetState(int expect?? int update) {
return unsafe.compareAndSwapInt(this?? stateOffset?? expect?? update);
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11