???????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);
}