ReentrantLock实现原理

在多线程操作过程中,锁是保证原子性和可见性的重要方式,synchronized关键字是隐式获取锁的方式,Lock类比synchronized关键字更加灵活,是显示获取锁的方式。本文将详细了解ReentrantLock类。

1. ReentrantLock基本使用

使用该类比较简单,初始化该类后,使用其提供的API去获取锁和释放锁,如下:

public class ReentrantLockDemo {
    private static Integer count = 0;
    private static Lock lock = new ReentrantLock();

    public static void increment() {
        lock.lock();
        count++;
        lock.unlock();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread(ReentrantLockDemo::increment).start();
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
    }
}

使用上述lock和unlock方法,能保证同时只有一个线程能获取锁,其他线程处于阻塞队列中,当释放锁后,其他线程能再次获取锁,以此来保证锁区域的原子性,也能保证前一个线程的操作对后续线程操作的可见性。

2. ReentrantLock底层实现

先来看一下源码中该类的结构图:
在这里插入图片描述
ReentrantLock继承了AQS(AbstractQueuedSynchronizer)类,并且定义了同步器对象Sync。AQS类是实现锁的基础对象,在该类中定义了线程的同步状态及一个FIFO的阻塞队列。
首先,来看ReentrantLock构造方法:

public ReentrantLock() {
    sync = new NonfairSync();
}

public ReentrantLock(boolean fair) {
    sync = fair ? new FairSync() : new NonfairSync();
}

通过上述构造方法构建使用的同步器,默认是采用非公平锁的方式,可以通过构造传参进行选择。公平锁严格遵守FIFO模型,最先进入阻塞队列的线程在锁释放后最先执行。而非公平锁在锁释放后,任意线程均可抢占锁。
lock()方法源码如下:

public void lock() {
    sync.lock();
}

先来分析非公平锁的方式:

static final class NonfairSync extends Sync {
    private static final long serialVersionUID = 7316153563782823691L;

    /**
     * Performs lock.  Try immediate barge, backing up to normal
     * acquire on failure.
     */
    final void lock() {
        if (compareAndSetState(0, 1))
            setExclusiveOwnerThread(Thread.currentThread());
        else
            acquire(1);
    }
    ...
}

在NonfairSync.lock()方法中,会调用Unsafe类的CAS方法修改线程状态,若能修改成功,则表示当前线程获得锁,会记录下当前线程信息;若CAS调用返回false,则调用AQS里面的acquire()方法

public final void acquire(int arg) {
    if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
        selfInterrupt();
}

这里的tryAcquire()会调用ReentrantLock#NonfairSync中的tryAcquire方法,源码如下:

protected final boolean  tryAcquire(int acquires) {
    return nonfairTryAcquire(acquires);
}

abstract static class Sync extends AbstractQueuedSynchronizer {
...
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;
}
...
}

在这个过程中,会再次判断线程的状态,若线程是无锁状态,则再次通过CAS获取锁。若当前线程再次获取锁,则直接增加获取锁的次数,这里是实现可重入锁的核心方法。
若还是获取不到锁,回到AQS中的acquire方法,会将当前线程组装一个Node节点,然后添加到阻塞队列中

public final void acquire(int arg) {
    if (!tryAcquire(arg) &&
        acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
        selfInterrupt();
}

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

private Node enq(final Node node) {
    for (;;) {
        Node t = tail;
        if (t == null) { // Must initialize
            if (compareAndSetHead(new Node()))
                tail = head;
        } else {
            node.prev = t;
            if (compareAndSetTail(t, node)) {
                t.next = node;
                return t;
            }
        }
    }
}

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

static void selfInterrupt() {
    Thread.currentThread().interrupt();
}
...
}

通过AQS类中定义的compareAndSetHead和compareAndSetTail方法,将获取锁失败的线程构建成Node,并添加至一个双向链表中,然后在添加至阻塞队列时,将当前线程阻塞

final boolean acquireQueued(final Node node, int arg) {
   boolean failed = true;
    try {
        boolean interrupted = false;
        for (;;) {
            ...
            if (shouldParkAfterFailedAcquire(p, node) &&
                parkAndCheckInterrupt())
                interrupted = true;
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}

private final boolean parkAndCheckInterrupt() {
    LockSupport.park(this);
    return Thread.interrupted();
}

这里会借助LockSupport.park阻塞当前线程。当锁释放之后才会唤醒处于阻塞的线程。

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

AQS类中的tryRelease()方法也是一个抽象方法,具体实现由子类进行实现,源码如下:

abstract static class Sync extends AbstractQueuedSynchronizer {
...
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);
    }
    // 将state状态复原
    setState(c);
    return free;
}
}

state记录了获取锁的次数,如果是重入锁,需要等当前线程将锁完全释放后,才会将独占锁的线程标识清空。
在AQS的release方法中,若当前线程释放锁,且阻塞队列中有节点时,则会唤醒处于阻塞状态的下一个线程

public final boolean release(int arg) {
    if (tryRelease(arg)) {
        Node h = head;
        if (h != null && h.waitStatus != 0)
            unparkSuccessor(h);
        return true;
    }
    return false;
}

private void unparkSuccessor(Node node) {
    int ws = node.waitStatus;
    if (ws < 0)
        compareAndSetWaitStatus(node, ws, 0);

    // 获取下一个节点,若下一个节点为空或者为取消状态,则从尾部往前找到当前节点后第一个未取消的节点
    Node s = node.next;
    if (s == null || s.waitStatus > 0) {
        s = null;
        for (Node t = tail; t != null && t != node; t = t.prev)
            if (t.waitStatus <= 0)
                s = t;
    }
    // 释放锁
    if (s != null)
        LockSupport.unpark(s.thread);
}

这里借助LockSupport.unpark唤醒处于双向链表中的下一个节点,直到所有的锁释放,程序执行结束。

而公平锁在获取锁时会检查阻塞队列中是否有值,若有值,则会将当前线程加入阻塞队列:

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

其他的操作与非公平锁一致。

posted @ 2021-07-13 09:59  生活咖啡  阅读(335)  评论(0编辑  收藏  举报