Semaphore源码解析

Semaphore

https://www.bilibili.com/video/BV1Ae411C7xr/

public class Semaphore implements java.io.Serializable

同Reetrantlock在Sync 继承AQS

abstract static class Sync extends AbstractQueuedSynchronizer

可以指定Sync是否是公平锁,默认非公平

permits为设置AQS内state数==可以共享线程数

public Semaphore(int permits) {
    sync = new NonfairSync(permits);
}
public void acquire() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg)
        throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
    if (tryAcquireShared(arg) < 0)
        doAcquireSharedInterruptibly(arg);
}

tryAcquireShared默认非公平锁实现去扣减许可数,如果remaining>=0,执行CAS设置,并直接返回remaining

如果remaining < 0,执行doAcquireSharedInterruptibly(arg);

final int nonfairTryAcquireShared(int acquires) {
    for (;;) {
        int available = getState();
        int remaining = available - acquires;
        if (remaining < 0 ||
            compareAndSetState(available, remaining))
            return remaining;
    }
}
private void doAcquireSharedInterruptibly(int arg)
    throws InterruptedException {
    //初始添加虚拟头节点 -> node
    final Node node = addWaiter(Node.SHARED);
    boolean failed = true;
    try {
        for (;;) {
            final Node p = node.predecessor();
            if (p == head) {
                int r = tryAcquireShared(arg);
                if (r >= 0) {
                    setHeadAndPropagate(node, r);
                    p.next = null; // help GC
                    failed = false;
                    return;
                }
            }
            if (shouldParkAfterFailedAcquire(p, node) &&
                //设置虚拟头节点为signal 然后当前线程阻塞
                parkAndCheckInterrupt())
                throw new InterruptedException();
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}

假设有4个线程,设置new Semaphore(2)

t1,t2,t3,t4,其中t1,t2运行

等待队列dual->t3->t4,dual和t3 的ws为signal状态,t3和t4为阻塞状态

t1释放共享锁

public final boolean (int arg) {
    //tryReleaseShared实现方法.增加state数量
    if (tryReleaseShared(arg)) {
        doReleaseShared();
        return true;
    }
    return false;
}
private void doReleaseShared() {
    for (;;) {
        Node h = head;
        if (h != null && h != tail) {
            int ws = h.waitStatus;
            if (ws == Node.SIGNAL) {
                //将dual头节点ws改为0
                if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                    continue;            // loop to recheck cases
                //释放t3节点
                unparkSuccessor(h);
            }
            else if (ws == 0 &&
                     !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                continue;                // loop on failed CAS
        }
        if (h == head)                   // loop if head changed
            break;
    }
}
private void unparkSuccessor(Node node) {
    int ws = node.waitStatus;
    if (ws < 0)
        compareAndSetWaitStatus(node, ws, 0);

    //t3=dual.next
    Node s = node.next;
    //t3!=null 并且 t3.ws=sigal
    if (s == null || s.waitStatus > 0) {
        s = null;
        for (Node t = tail; t != null && t != node; t = t.prev)
            if (t.waitStatus <= 0)
                s = t;
    }
    if (s != null)
        //释放t3线程
        LockSupport.unpark(s.thread);
}

如果此时运行的线程t2也释放,t2线程执行代码,此时执行完毕t4线程仍阻塞

private void doReleaseShared() {
    for (;;) {
        Node h = head;
        if (h != null && h != tail) {
            int ws = h.waitStatus;
            //此时的head ws=0
            if (ws == Node.SIGNAL) {
                if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                    continue;            // loop to recheck cases
                unparkSuccessor(h);
            }
            //所以设置head ws=PROPAGATE
            else if (ws == 0 &&
                     !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                continue;                // loop on failed CAS
        }
		//然后退出循环
        if (h == head)                   // loop if head changed
            break;
    }
}

但是t1线程执行unpark,回到t1线程

doAcquireShared方法parkAndCheckInterrupt()内退出阻塞

进行循环

private void doAcquireShared(int arg) {
    final Node node = addWaiter(Node.SHARED);
    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);
    }
}

node是t3节点

private void setHeadAndPropagate(Node node, int propagate) {
    Node h = head; // Record old head for check below
    //已设置t3为head
    setHead(node);
 	//此时h是老头节点,h!=null并且h.ws=propagate 满足h.waitStatus < 0
    if (propagate > 0 || h == null || h.waitStatus < 0 ||
        (h = head) == null || h.waitStatus < 0) {
        Node s = node.next;
        if (s == null || s.isShared())
            //释放t4节点
            doReleaseShared();
    }
}
posted @   8023渡劫  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示