AQS源码解析-独占式获取锁响应中断、独占式超时获取锁

独占式获取锁响应中断--acquireInterruptibly(int arg)

AQS提供了acquire(int arg)方法以供独占式获取同步状态,但是该方法对中断不响应,对线程进行中断操作后,该线程会依然位于CLH同步队列中等待着获取同步状态。为了响应中断,AQS提供了acquireInterruptibly(int arg)方法,该方法在等待获取同步状态时,如果当前线程被中断了,会立刻响应中断抛出异常InterruptedException。

//模板方法,独占式获取锁(同步状态)响应中断
public final void acquireInterruptibly(int arg)
        throws InterruptedException {
    //判断是否被中断
    if (Thread.interrupted())
        throw new InterruptedException();
    if (!tryAcquire(arg))
        doAcquireInterruptibly(arg);
}
复制代码

private void doAcquireInterruptibly(int arg)
        throws InterruptedException {
    final Node node = addWaiter(Node.EXCLUSIVE);
    boolean failed = true;
    try {
        for (; ; ) {
            final Node p = node.predecessor();
            if (p == head && tryAcquire(arg)) {
                setHead(node);
                p.next = null; // help GC
                failed = false;
                return;
            }
            if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                //与acquireQueued唯一的区别就是这里,如果中断了就抛出异常
                throw new InterruptedException();
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}
复制代码

独占式超时获取锁--tryAcquireNanos(int arg,long timeout)

AQS除了提供上面两个方法外,还提供了一个增强版的方法:tryAcquireNanos(int arg,long nanos)。该方法为acquireInterruptibly方法的进一步增强,它除了响应中断外,还有超时控制。即如果当前线程没有在指定时间内获取同步状态,则会返回false,否则返回true。如下:

public final boolean tryAcquireNanos(int arg, long nanosTimeout)
        throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
    return tryAcquire(arg) ||
            doAcquireNanos(arg, nanosTimeout);
}
复制代码
private boolean doAcquireNanos(int arg, long nanosTimeout)
        throws InterruptedException {
    //如果nanosTimeout小于0直接返回
    if (nanosTimeout <= 0L)
        return false;
    //通过将当前时间+设置的超时时间=超时的时间点
    final long deadline = System.nanoTime() + nanosTimeout;
    final Node node = addWaiter(Node.EXCLUSIVE);
    boolean failed = true;
    try {
        for (; ; ) {
            final Node p = node.predecessor();
            if (p == head && tryAcquire(arg)) {
                setHead(node);
                p.next = null; // help GC
                failed = false;
                return true;
            }
            //超时时间点-当前时间=剩余的超时时间
            nanosTimeout = deadline - System.nanoTime();
            //如果剩余的超时时间小于0说明已超时,则返回
            if (nanosTimeout <= 0L)
                return false;
            //如果没有超时则等待nanosTimeout纳秒
            //注:该线程会直接从LockSupport.parkNanos(this, nanosTimeout)中返回
            //LockSupport是JUC提供的一个阻塞和唤醒的工具类
            if (shouldParkAfterFailedAcquire(p, node) &&
                    //如果nanosTimeout大于spinForTimeoutThreshold,就需要休眠nanosTimeout
                    nanosTimeout > spinForTimeoutThreshold)
                LockSupport.parkNanos(this, nanosTimeout);
            //如果nanosTimeout小于等于spinForTimeoutThreshold,那么就不需要休眠了,直接进入快速自旋
            // 因为spinForTimeoutThreshold已经很短了,如果在更短的超时时间内还要休眠,可能会导致超时时间不那么准确
            //线程的状态切换是需要时间的
            
            //判断线程是否已经被中断了
            if (Thread.interrupted())
                throw new InterruptedException();
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}
复制代码

与独占式获取锁响应中断的不同就在加入到同步队列之后对超时时间的计算
在这里插入图片描述

猜你喜欢

转载自juejin.im/post/7042173768734605319
今日推荐