• AbstractQueuedSynchronizer源码解析之ReentrantLock(二)


    上篇文章分析了ReentrantLock的lock,tryLock,unlock方法,继续分析剩下的方法,首先开始lockInterruptibly,先看其API说明:
    lockInterruptiblypublic void lockInterruptibly()
    throws Acquires the lock unless the current thread is .

    Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.

    If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.

    If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:

    • The lock is acquired by the current thread; or
    • Some other thread the current thread.

    文档说明,只要线程没有处于interrupted状态,就尝试去获取锁,直到获取到锁,或者被interrupted。下面看其源码实现:
    public void lockInterruptibly() throws InterruptedException {
    sync.acquireInterruptibly(1);
    }
    acquireInterruptibly方法在AbstractQueuedSynchronizer中:
    /**
    * Acquires in exclusive mode, aborting if interrupted. Implemented by first
    * checking interrupt status, then invoking at least once
    * {@link #tryAcquire}, returning on success. Otherwise the thread is
    * queued, possibly repeatedly blocking and unblocking, invoking

    * {@link #tryAcquire} until success or the thread is interrupted. This
    * method can be used to implement method {@link Lock#lockInterruptibly}.

    * @param arg
    *            the acquire argument. This value is conveyed to
    *            {@link #tryAcquire} but is otherwise uninterpreted and can
    *            represent anything you like.
    * @throws InterruptedException
    *             if the current thread is interrupted
    */
    public final void acquireInterruptibly(int arg) throws InterruptedException {
    if (Thread.interrupted())
    throw new InterruptedException();
    if (!tryAcquire(arg))
    doAcquireInterruptibly(arg);
    }
    首先检查interrupted状态,调用tryAcquire方法,失败则:
    /**
    * Acquires in exclusive interruptible mode.

    * @param arg
    *            the acquire argument
    */
    private void doAcquireInterruptibly(int arg) throws InterruptedException {
    final Node node = addWaiter(Node.EXCLUSIVE);
    try {
    for (;;) {
    final Node p = node.predecessor();
    if (p == head && tryAcquire(arg)) {
    setHead(node);
    p.next = null; // help GC
    return;
    }
    if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt())
    break;
    }
    } catch (RuntimeException ex) {
    cancelAcquire(node);
    throw ex;
    }
    // Arrive here only if interrupted
    cancelAcquire(node);
    throw new InterruptedException();
    }
    这段代码比较眼熟,这也是个阻塞方法,阻塞至node前继是head且exclusively owned synchronizer则返回,如果p is blocked and interrupted,这时线程处于interrupted状态,所以中止.

           最后看一下ReentrantLock的newCondition方法,看这个方法之前先看看Doug Lea的论文《The java.util.concurrent Synchronizer Framework》,上面描述如下:
    A ConditionObject uses the same internal queue nodes as synchronizers, but maintains them on a separate condition queue.The signal operation is implemented as a queue transfer from the condition queue to the lock queue, without necessarily waking up the signalled thread before it has re-acquired its lock.The basic await operation is:
    create and add new node to condition queue;
    release lock;
    block until node is on lock queue;
    re-acquire lock;
    And the signal operation is:
    transfer the first node from condition queue to lock queue;
    Because these operations are performed only when the lock is held, they can use sequential linked queue operations (using a nextWaiter field in nodes) to maintain the condition queue.
    The transfer operation simply unlinks the first node from the condition queue.queue, and then uses CLH insertion to attach it to the lock queue.
    继续看代码:
    public Condition newCondition() {
    return sync.newCondition();
    }
    newCondition在Sync类中:
    final ConditionObject newCondition() {
    return new ConditionObject();
    }
    直接返回一个ConditionObject 对象,ConditionObject 类是AbstractQueuedSynchronizer.java文件中。


    来看看其声明的变量:
    private static final long serialVersionUID = 1173984872572414699L;
    /** First node of condition queue. */
    private transient Node firstWaiter;
    /** Last node of condition queue. */
    private transient Node lastWaiter;
    声明两个节点,通过后面的方法知道,这是一个单向链表。继续看其await方法:
    public final void await() throws InterruptedException {
    if (Thread.interrupted())
    throw new InterruptedException();
    Node node = addConditionWaiter();
    int savedState = fullyRelease(node);
    int interruptMode = 0;
    while (!isOnSyncQueue(node)) {
    LockSupport.park(this);
    if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
    break;
    }
    if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
    interruptMode = REINTERRUPT;
    if (node.nextWaiter != null)
    unlinkCancelledWaiters();
    if (interruptMode != 0)
    reportInterruptAfterWait(interruptMode);
    }
    检查状态后会调用addConditionWaiter方法:
    /**
    * Adds a new waiter to wait queue.

    * @return its new wait node
    */
    private Node addConditionWaiter() {
    Node t = lastWaiter;
    // If lastWaiter is cancelled, clean out.
    if (t != null && t.waitStatus != Node.CONDITION) {
    unlinkCancelledWaiters();
    t = lastWaiter;
    }
    Node node = new Node(Thread.currentThread(), Node.CONDITION);
    if (t == null)
    firstWaiter = node;
    else
    t.nextWaiter = node;
    lastWaiter = node
    ;
    return node;
    }
    继续看unlinkCancelledWaiters方法:
    /**
    Unlinks cancelled waiter nodes from condition queue. Called only
    * while holding lock. This is called when cancellation occurred during
    * condition wait, and upon insertion of a new waiter when lastWaiter is
    * seen to have been cancelled
    . This method is needed to avoid garbage
    * retention in the absence of signals. So even though it may require a
    * full traversal, it comes into play only when timeouts or
    * cancellations occur in the absence of signals. It traverses all nodes
    * rather than stopping at a particular target to unlink all pointers to
    * garbage nodes without requiring many re-traversals during
    * cancellation storms.
    */
    private void unlinkCancelledWaiters() {
    Node t = firstWaiter;
    Node trail = null;
    while (t != null) {
    Node next = t.nextWaiter;
    if (t.waitStatus != Node.CONDITION) {
    t.nextWaiter = null; //next=null
    if (trail == null)
    firstWaiter = next;
    else
    trail.nextWaiter = next;//tail.nextWaiter=null
    if (next == null)
    lastWaiter = trail;
    } else
    trail = t;
    t = next;
    }
    }
    遍历链表,如果某结点状态不为Node.CONDITION,则移除当前结点及后面的全部结点
    addConditionWaiter方法首先会检查lastWaiter是否已经cancelled,是则清除链中cancelled的结点,否则添加当前结点为lastWaiter。

    继续看fullyRelease方法:
    /**
    * Invokes release with current state value; returns saved state. Cancels
    * node and throws exception on failure.

    * @param node
    *            the condition node for this wait
    * @return previous sync state
    */
    final int fullyRelease(Node node) {
    try {
    int savedState = getState();
    if (release(savedState))
    return savedState;
    } catch (RuntimeException ex) {
    node.waitStatus = Node.CANCELLED;//如果发生异常则将当前结点为置于CANCELLED状态
    throw ex;
    }
    // reach here if release fails
    node.waitStatus = Node.CANCELLED;
    throw new IllegalMonitorStateException();
    }
    调用release方法交出synchronized排它持有权并unblock sync queue下一个结点,最后返回savedState 值
    继续看下面的方法:
    /**
    * Returns true if a node, always one that was initially placed on a
    * condition queue, is now waiting to reacquire on sync queue.

    * @param node
    *            the node
    * @return true if is reacquiring
    */
    final boolean isOnSyncQueue(Node node) {
    if (node.waitStatus == Node.CONDITION || node.prev == null)
    return false; //node在condition queue中
    if (node.next != null) // If has successor, it must be on queue
    return true; //node有前继结点也有后续结点,那么它在sync queue上但node结点不是tail
    /*
    * node.prev can be non-null, but not yet on queue because the CAS to
    * place it on queue can fail. So we have to traverse from tail to make
    * sure it actually made it. It will always be near the tail in calls to
    * this method, and unless the CAS failed (which is unlikely), it will
    * be there, so we hardly ever traverse much.
    */
    return findNodeFromTail(node);
    }

    /**
    Returns true if node is on sync queue by searching backwards from tail.
    * Called only when needed by isOnSyncQueue.

    * @return true if present
    */
    private boolean findNodeFromTail(Node node) {
    Node t = tail;
    for (;;) {
    if (t == node)
    return true;
    if (t == null)
    return false;
    t = t.prev;//从tail结点开始,逆序遍历查找node结点,成功则返回true
    }
    }
    await会在while循环中判断:
    /**
    * Checks for interrupt, returning THROW_IE if interrupted before
    * signalled, REINTERRUPT if after signalled, or 0 if not interrupted.
    */
    private int checkInterruptWhileWaiting(Node node) {
    return (Thread.interrupted()) ? ((transferAfterCancelledWait(node)) ? THROW_IE : REINTERRUPT) : 0;
    }
    /**
    * Transfers node, if necessary, to sync queue after a cancelled wait.
    * Returns true if thread was cancelled before being signalled.

    * @param current
    *            the waiting thread
    * @param node
    *            its node
    * @return true if cancelled before the node was signalled.
    */
    final boolean transferAfterCancelledWait(Node node) {
    if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
    enq(node);//如果node状态为CONDITION,插入了sync queue尾部
    return true;
    }
    /*
    * If we lost out to a signal(), then we can't proceed until it finishes
    * its enq(). Cancelling during an incomplete transfer is both rare and
    * transient, so just spin.
    */
    while (!isOnSyncQueue(node))//如果node不在sync queue中,那么让其他线程执行
    Thread.yield();
    return false;
    }
    如果当前node状态为Node.CONDITION,则会把该node添加到sync queue;如果当前线程interrupted,返回THROW_IE 
    现在回顾一下await方法所进行的操作:
    1.首先调用addConditionWaiter方法将当前线程以Node.CONDITION模式添加到condition queue中,如果队列lastWaiter处于cancelled状态,遍历队列移除cancelled状态的结点
    2.调用fullyRelease方法交出synchronizer排它持有权并unpark sync queue的head后续结点,并返回之前的state值
    3.循环判断当前node是否在sync queue上,不在则park当前线程,并检测当前线程是否处于interrupted状态,是则中断循环
    4.调用acquireQueued阻塞方法,如果当前线程在signalled之前interrupted,设置interruptMode
    5.如果node nextWaiter
    不为空,则移除condition queue上cancelled的结点。
    6.最后检测interruptMode,抛出异常或interrupt自身

           继续看其signal方法:
    /**
    * Moves the longest-waiting thread, if one exists, from the wait queue
    * for this condition to the wait queue for the owning lock.
    *
    * @throws IllegalMonitorStateException
    *             if {@link #isHeldExclusively} returns {@code false}
    */
    public final void signal() {
    if (!isHeldExclusively())
    throw new IllegalMonitorStateException();
    Node first = firstWaiter;
    if (first != null)
    doSignal(first);//取出condition queue第一个结点
    }       
    检测当前线程是否exclusively owned synchronizer,调用doSignal方法:
    /**
    * Removes and transfers nodes until hit non-cancelled one or null.
    * Split out from signal in part to encourage compilers to inline the
    * case of no waiters.

    * @param first
    *            (non-null) the first node on condition queue
    */
    private void doSignal(Node first) {
    do {
    if ((firstWaiter = first.nextWaiter) == null)
    lastWaiter = null;
    first.nextWaiter = null;
    } while (!transferForSignal(first) && (first = firstWaiter) != null); 
    //注意这个循环条件,每次都将firstWaiter赋给first,这样保证操作的始终是队列中第一个结点即等待时间最长的线程
    }
    遍历condition queue,如果存在空结点,则null lastWaiter,
    /**
    * Transfers a node from a condition queue onto sync queue. Returns true if
    * successful.

    * @param node
    *            the node
    * @return true if successfully transferred (else the node was cancelled
    *         before signal).
    */
    final boolean transferForSignal(Node node) {
    /*
    * If cannot change waitStatus, the node has been cancelled.
    */
    if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
    return false;//如果不是CONDITION状态,不会transfer node到sync queue上

    /*
    * Splice onto queue and try to set waitStatus of predecessor to
    * indicate that thread is (probably) waiting. If cancelled or attempt
    * to set waitStatus fails, wake up to resync (in which case the
    * waitStatus can be transiently and harmlessly wrong).
    */
    Node p = enq(node);
    int c = p.waitStatus;
    if (c > 0 || !compareAndSetWaitStatus(p, c, Node.SIGNAL))
    LockSupport.unpark(node.thread);
    return true;
    }
    如果当前node状态为Node.CONDITION,则将其插入到sync queue中并返回状态,如果状态值为cancelled,或者状态有变化,则unpark node所含的线程。

            现在回顾一下singal方法所进行的操作
    1.检测
    2.循环,反复调用transferForSignal操作condition queue中第一个结点,直到成功。

    最后再看看signalAll方法的源码
    /**
    * Moves all threads from the wait queue for this condition to the wait
    * queue for the owning lock.

    * @throws IllegalMonitorStateException
    *             if {@link #isHeldExclusively} returns {@code false}
    */
    public final void signalAll() {
    if (!isHeldExclusively())
    throw new IllegalMonitorStateException();
    Node first = firstWaiter;
    if (first != null)
    doSignalAll(first);
    }

    /**
    * Removes and transfers all nodes.

    * @param first
    *            (non-null) the first node on condition queue
    */
    private void doSignalAll(Node first) {
    lastWaiter = firstWaiter = null;
    do {
    Node next = first.nextWaiter;
    first.nextWaiter = null;
    transferForSignal(first);
    first = next;
    } while (first != null);
    }
    遍历调用transferForSignal unpark结点所含的线程。
    ReentrantLock的源码分析就到这里了,接下来想继续看看Semaphore,Latches等源码。

  • 相关阅读:
    sql
    po bo vo java bean
    jdk面试
    Bean 参数时间 设置
    kafka demo
    spring注解 动态修改注解的值
    参考资料
    Centos 编译带调试信息的libevent
    mysql 库表整体相关查询
    MySQL安装(linux)
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959131.html
Copyright © 2020-2023  润新知