concurrent-6-AQS-Semaphore

Semaphore#acquire

 public void acquire() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);   //请求资源
    }

AQS#acquireSharedInterruptibly

 public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (tryAcquireShared(arg) < 0)  //获取资源失败
            doAcquireSharedInterruptibly(arg);   //循环cas 获取资源
    }

Semaphore#nonfairTryAcquireShared

   final int nonfairTryAcquireShared(int acquires) {
            for (;;) {
                int available = getState();  //获取可用状态
                int remaining = available - acquires;
                if (remaining < 0 ||     //如果剩余状态大于0 则尝试cas操作 否则 return
                    compareAndSetState(available, remaining)) //cas获取资源
                    return remaining;
            }
        }

AQS#doAcquireSharedInterruptibly

//doAcquireSharedInterruptibly 类似,响应中断
private void doAcquireShared(int arg) {
        final Node node = addWaiter(Node.SHARED);  //将share节点入队列
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {  //队列中只有头节点能获取资源
                    int r = tryAcquireShared(arg);  //尝试获取读锁资源
                    if (r >= 0) {  //资源获取成功
                        setHeadAndPropagate(node, r);  //设置为头节点,并尝试唤醒下一个共享节点
                        p.next = null; // help GC
                        if (interrupted)   //判断中断
                            selfInterrupt();
                        failed = false;
                        return;
                    }
                }
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

Semaphore#release

 public void release() {
        sync.releaseShared(1);  //释放资源
    }

Semaphore#tryReleaseShared

protected final boolean tryReleaseShared(int releases) {
            for (;;) {
                int current = getState();
                int next = current + releases;  //释放资源则可用数增加 releases
                if (next < current) // overflow
                    throw new Error("Maximum permit count exceeded");
                if (compareAndSetState(current, next))   //cas操作释放资源
                    return true;
            }
        }

AQS#doReleaseShared

private void doReleaseShared() {
        /*
         * Ensure that a release propagates, even if there are other
         * in-progress acquires/releases.  This proceeds in the usual
         * way of trying to unparkSuccessor of head if it needs
         * signal. But if it does not, status is set to PROPAGATE to
         * ensure that upon release, propagation continues.
         * Additionally, we must loop in case a new node is added
         * while we are doing this. Also, unlike other uses of
         * unparkSuccessor, we need to know if CAS to reset status
         * fails, if so rechecking.
         */
        for (;;) {
            Node h = head;
            //最终状态 signal = 》0 = 》 PROPAGATE  经过一步0的操作时因为unparkSuccessor 中会有cas设置为0状态
            if (h != null && h != tail) {
                int ws = h.waitStatus;  //获取头节点的状态
                if (ws == Node.SIGNAL) {  //如果节点的状态为唤醒,则置为0
                    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) //失败则跳出循环重新开始
                        continue;            // loop to recheck cases
                    unparkSuccessor(h);   //唤醒下一个节点
                } else if (ws == 0 &&
                         !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))  //将节点设置为可传播
                    continue;                // loop on failed CAS
            }
            //如果头节点被更换,即下一个节点被唤醒
            if (h == head)                   // loop if head changed
                break;
        }
    }
内容概要:该论文深入研究了液压挖掘机动臂下降势能回收技术,旨在解决传统液压挖掘机能耗高的问题。提出了一种新型闭式回路势能回收系统,利用模糊PI自整定控制算法控制永磁无刷直流电动机,实现了变转速容积调速控制,消除了节流和溢流损失。通过建立数学模型和仿真模型,分析了不同负载下的系统性能,并开发了试验平台验证系统的高效性和节能效果。研究还涵盖了执行机构能量分布分析、系统元件参数匹配及电机控制性能优化,为液压挖掘机节能技术提供了理论和实践依据。此外,通过实验验证,该系统相比传统方案可降低28%的能耗,控制系统响应时间缩短40%,为工程机械的绿色化、智能化发展提供了关键技术支撑。 适合人群:从事工程机械设计、制造及维护的工程师和技术人员,以及对液压系统节能技术感兴趣的科研人员。 使用场景及目标:①理解液压挖掘机闭式回路动臂势能回收系统的原理和优势;②掌握模糊PI自整定控制算法的具体实现;③学习如何通过理论建模、仿真和实验验证来评估和优化液压系统的性能。 其他说明:此研究不仅提供了详细的理论分析和数学建模,还给出了具体的仿真代码和实验数据,便于读者在实际工作中进行参考和应用。研究结果表明,该系统不仅能显著提高能源利用效率,还能延长设备使用寿命,降低维护成本,具有重要的工程应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值