设为首页 加入收藏

TOP

10分钟从源码级别搞懂AQS(AbstractQueuedSynchronizer)(五)
2023-09-09 10:25:55 】 浏览:148
Tags:10分 钟从源 别搞懂 AQS AbstractQueuedSynchronizer
p;              parkAndCheckInterrupt())                    //被唤醒后检查到被中断时抛出中断异常                    throw new InterruptedException();           }       } finally {            if (failed)                cancelAcquire(node);       }   }

响应中断的获取同步状态被中断时会直接抛出中断异常,而不响应的是自己中断

响应超时

响应超时的获取同步状态使用tryAcquireNanos 超时时间为纳秒级别

public final boolean tryAcquireNanos(int arg, long nanosTimeout)
        throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
    return tryAcquire(arg) ||
        doAcquireNanos(arg, nanosTimeout);
}

可以看出响应超时同时也会响应中断

doAcquireNanos也与原过程类似

    private boolean doAcquireNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        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();
                if (nanosTimeout <= 0L)
                    //已超时
                    return false;
                if (shouldParkAfterFailedAcquire(p, node) &&
                    //大于1ms
                    nanosTimeout > spinForTimeoutThreshold)
                    //超时等待
                    LockSupport.parkNanos(this, nanosTimeout);
                //响应中断
                if (Thread.interrupted())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
           &n
首页 上一页 2 3 4 5 6 7 下一页 尾页 5/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Nacos 注册中心的设计原理:让你.. 下一篇电商类面试问题--01Elasticsearch..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目