之前文章写AQS的时候有说过公平锁的代码 这里以非公平锁来看一下

面试的时候设计模式:模板方法 在AQS里体现的淋漓尽致 ,你要是从设计模式扯到这里,然后你正好又看过AQS在ReentrantLock中的实现,那你就可以让面试官眼前一亮

Lock lock  =  new ReentrantLock(false);
lock.lock();

/**
     * 申请锁
     *
     * <p>Acquires the lock if it is not held by another thread and returns
     * immediately, setting the lock hold count to one.
     *  如果没有其他线程持有这个锁,就马上返回,并设置锁持有数为1
     * <p>If the current thread already holds the lock then the hold
     * count is incremented by one and the method returns immediately.
     * 如果当前线程持有锁,就吧持有数量+1(可重入) 然后立即返回 
     * <p>If the lock is held by another thread then the
     * current thread becomes disabled for thread scheduling
     * purposes and lies dormant until the lock has been acquired,
     * at which time the lock hold count is set to one.
     * 如果锁被其他线程持有,当前线程对于调度就不可用,然后睡着, 直到获取锁,然后把锁持有数改为1 
     */
// ReentrantLock.class
public void lock() {
    sync.lock();
}
// NonfairSync.class
final void lock() {
    // 先抢一下(上一篇说的 :小强二话不说先看看能不能跟老板选饼)
    if (compareAndSetState(0, 1))
        setExclusiveOwnerThread(Thread.currentThread());
    else
        // 如果没有抢到 再去申请
        acquire(1);
}
// AbstractQueuedSynchronizer.class
 public final void acquire(int arg) {
        // 这个tryAcquire会再抢一次(小强还抱着插队的幻想)
        if (!tryAcquire(arg) &&
            // 抢不到 就后边排队吧
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
 }

 // ReentrantLock.class
 protected final boolean tryAcquire(int acquires) {
            return nonfairTryAcquire(acquires);
 }

// ReentrantLock.class 
// 这里需要了解AQS 我其他文章有写 可以去看
final boolean nonfairTryAcquire(int acquires) {
    // 获取当前线程
    final Thread current = Thread.currentThread();
    // 获取state标志位
    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;
}

图片说明