AQS源码解读

news/2024/7/24 13:34:30 标签: java

 retrantlock:

A、B、C3个线程,假设A线程lock()时候拿到了锁,state被A设置成了1。

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

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

 此时B和C调用lock时候就会走else,调用acquire(1);

我们可用看见这里的B和C会尝试获取锁,没有获取到就会把当前线程封装成node节点放到CLH双向队列里。让后阻断当前线程

AQS的 tryAcquire(),

实际是调用RetrantLock的非公平nonfairTryAcquire();

 

 此时 int c = getState();  c是1   并且current == getExclusiveOwnerThread()条件不成立。因为此时线程A占有锁,所以current此时是线程B 与 getExclusiveOwnerThread()是线程A,并不相等,返回false。

else if (current == getExclusiveOwnerThread()) {
    int nextc = c + acquires;
    if (nextc < 0) // overflow
        throw new Error("Maximum lock count exceeded");
    setState(nextc);
    return true;
}

上面这段代码以上是假设B来了A刚好释放了锁,此时,突然A又来抢到了锁,这个时候current就是A线程和 getExclusiveOwnerThread()也是线程A,相等。又将state设置为1,并且占用锁。

将当前线程封装成node节点放到队列里

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

你会发现底层调用了unsafe的接口 ,头指针主要是用于入队。

new Node()作为头节点 (第一个节点)

 

 

acquireQueued()干了一件什么事呢?

private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;
        if (ws == Node.SIGNAL)
            /*
             * This node has already set status asking a release
             * to signal it, so it can safely park.
             */
            return true;
        if (ws > 0) {
            /*
             * Predecessor was cancelled. Skip over predecessors and
             * indicate retry.
             */
            do {
                node.prev = pred = pred.prev;
            } while (pred.waitStatus > 0);
            pred.next = node;
        } else {
            /*
             * waitStatus must be 0 or PROPAGATE.  Indicate that we
             * need a signal, but don't park yet.  Caller will need to
             * retry to make sure it cannot acquire before parking.
             */
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;
    }

 

没有抢到锁就阻塞,LockSupport.park(this);线程挂起不会继续往下执行

 

compareAndSetWaitStatus(pred, ws, Node.SIGNAL);

 unlock()f释放锁;

 

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

 

释放锁实现:

 int c = getState() - releases; state的值此时是1,releases值也是1  所以c就变成了0,就释放了锁

 setExclusiveOwnerThread(null);设置当前占用线程为null,就是没有线程占用。

 setState(c);将state状态重新设置为0.

 


http://www.niftyadmin.cn/n/20232.html

相关文章

VMware17虚拟机安装及Linux系统搭建(详细版)

✅作者简介&#xff1a;大家好&#xff0c;我是小杨 &#x1f4c3;个人主页&#xff1a;「小杨」的csdn博客 &#x1f433;希望大家多多支持&#x1f970;一起进步呀&#xff01; &#x1f4dc;前言&#xff1a; VMware是一个虚拟PC的软件&#xff0c;可以在现有的操作系统上虚…

基于Xlinx的时序分析与约束(5)----衍生时钟约束

衍生时钟约束语法 衍生时钟&#xff08;Generated Clocks&#xff0c;又称为生成时钟&#xff09;是指由设计中已有的主时钟通过倍频、分频或者相移等操作后产生的新的时钟信号&#xff0c;如由MMCM或PLL或由组合逻辑生成的倍、分频时钟信号。 衍生时钟约束必须指定时钟源&…

关于 Linux 中 firewalld 的一些笔记整理

写在前面 嗯,今天和小伙伴们分享一些 firewall 的笔记内容涉及&#xff1a; zone 的介绍具和具体规则的添加服务&#xff0c;端口和协议&#xff0c;ICMP 阻塞&#xff0c;SNAT/DNAT,IP伪装&#xff0c;端口转发等Demofirewall 离线命令(服务未启动规则预设方式) 理解不足小伙伴…

【学习打卡07】 可解释机器学习笔记之Shape+Lime代码实战

可解释机器学习笔记之ShapeLime代码实战 文章目录可解释机器学习笔记之ShapeLime代码实战基于Shapley值的可解释性分析使用Pytorch对MNIST分类可解释性分析使用shap的Deep Explainer进行可视化使用Pytorch对预训练ImageNet图像分类可解释性分析指定单个预测类别指定多个预测类别…

TCP 的可靠传输(计算机网络-运输层)

TCP的可靠传输 因特网的网络层服务是不可靠的 TCP在IP的不可靠的&#xff1a;尽最大努力服务的基础上实现了一种可靠的数据传输服务 TCP采用的可靠传输机制&#xff1a;差错检测、序号、确认、超时重传、滑动窗口等 互联网环境中端到端的时延往往是比较大的&#xff1a;采用…

DAY5 Recommended system cold startup problem

推荐系统的冷启动问题 推荐系统冷启动概念 ⽤户冷启动&#xff1a;如何为新⽤户做个性化推荐物品冷启动&#xff1a;如何将新物品推荐给⽤户&#xff08;协同过滤&#xff09;系统冷启动&#xff1a;⽤户冷启动物品冷启动本质是推荐系统依赖历史数据&#xff0c;没有历史数据⽆…

【车载开发系列】UDS诊断---DTC故障码进阶部分回顾

【车载开发系列】UDS诊断—DTC故障码进阶部分回顾 UDS诊断---DTC故障码进阶部分回顾【车载开发系列】UDS诊断---DTC故障码进阶部分回顾一.操作周期(Operation Cyle)的概念二.监控周期(Monitoring cycle)的概念三.老化计数(Aging Counter)的概念四.老化阈值(Aging Threshold)的概…

【机器学习】模型评估与选择

模型评估与选择 目录一、评估方法1、留出法2、交叉验证法3、自助法二、性能度量1、错误率与准确率2、查准率、查全率阈值对查准率、查全率的影响3、F1度量&#xff08;基于查准率与查全率的调和平均&#xff09;4、P-R Curve5、ROC CurvePRC和ROC的选用准则PRC和ROC的差异6、代…