美文网首页
AQS之CountDownLatch

AQS之CountDownLatch

作者: 有章 | 来源:发表于2018-08-19 10:39 被阅读0次

    CountDownLatch在Jdk1.5中引入,通过内部实现继承了AbstractQueuedSynchronizer的Sync类,并实现了tryAcquireShared(arg)、tryReleaseShared方法,使用方式如下

    public class CountDownLatchDemo {
        public static void main(String[] args) throws InterruptedException {
            CountDownLatch latch=new CountDownLatch(3);
            for (int i=0;i<3;i++){
                new Thread(()->{
                    System.out.println("thread "+Thread.currentThread().getName()+" entry");
                    latch.countDown();
                    System.out.println("thread "+Thread.currentThread().getName()+" exit");
                },"T"+i).start();
            }
    
            latch.await();
            System.out.println("main thread end");
        }
    }
    

    创建了一个初始值为3的CountDownLatch对象latch,然后创建了3个线程,每个线程执行时都会执行latch.countDown()使计数器的值减1,而主线程在执行到latch.await()时会等待直到计数器的值为0。输出的结果如下:

    thread T0 entry
    thread T2 entry
    thread T1 entry
    thread T2 exit
    thread T0 exit
    thread T1 exit
    main thread end
    

    await方法:

        public void await() throws InterruptedException {
            sync.acquireSharedInterruptibly(1);
        }
    

    这是countDownLatch实现的方法,通过调用sync中的acquireSharedInterruptibly方法,实际调用了AQS框架中的acquireSharedInterruptibly,而coundoownlatch本身只需要实现tryAcquireShared方法即可。
    acquireSharedInterruptibly方法:

    public final void acquireSharedInterruptibly(int arg)
                throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
            if (tryAcquireShared(arg) < 0)
                doAcquireSharedInterruptibly(arg);
        }
    

    coundownlatch自己实现的tryAcquireShared方法

      protected int tryAcquireShared(int acquires) {
                return (getState() == 0) ? 1 : -1;
            }
    根据状态来判断,如果state等于0说明计数器为0了,返回1表示成功,否则返回-1表示失败,需要放入队列中继续等待state变为0
    

    当state!=0时,执行doAcquireSharedInterruptibly方法:
    1.创建共享的node节点,并加入等待队列
    2.获取前置节点p,如果等于head节点,则尝试获取锁,如果state==0(r>=0),则将node节点设置为head,并向后面节点传播
    3.如果不为head,则自旋,找到安全点后park自己,并在唤醒后返回是否被中断

    private void doAcquireSharedInterruptibly(int arg)
            throws InterruptedException {
            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) &&
                        parkAndCheckInterrupt())
                        throw new InterruptedException();
                }
            } finally {
                if (failed)
                    cancelAcquire(node);
            }
        }
    
    private void setHeadAndPropagate(Node node, int propagate) {
            Node h = head; // Record old head for check below
            setHead(node);
            /*
             * Try to signal next queued node if:
             *   Propagation was indicated by caller,
             *     or was recorded (as h.waitStatus either before
             *     or after setHead) by a previous operation
             *     (note: this uses sign-check of waitStatus because
             *      PROPAGATE status may transition to SIGNAL.)
             * and
             *   The next node is waiting in shared mode,
             *     or we don't know, because it appears null
             *
             * The conservatism in both of these checks may cause
             * unnecessary wake-ups, but only when there are multiple
             * racing acquires/releases, so most need signals now or soon
             * anyway.
             */
    CDL如果获取锁成功,则propagate=1
    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())
                    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;
                if (h != null && h != tail) {
                    int ws = h.waitStatus;
                    if (ws == Node.SIGNAL) {
    如果当前节点ws为-1,表明需要唤醒后继节点
                        if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                            continue;            // loop to recheck cases
                        unparkSuccessor(h);
                    }
    ws==0,表明节点初始化状态,需要设置为Node.PRoPagate状态
                    else if (ws == 0 &&
                             !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                        continue;                // loop on failed CAS
                }
    如果节点发生变化,自旋
                if (h == head)                   // loop if head changed
                    break;
            }
        }
    

    CDL的countDown()方法

    //CountDownLatch
    public void countDown() {
        sync.releaseShared(1);
    }
    
    //AQS
        public final boolean releaseShared(int arg) {
            if (tryReleaseShared(arg)) {
                doReleaseShared();
                return true;
            }
            return false;
        }
    
    //CDL中重写了tryReleaseShared
    1.自旋的进行CAS操作
    2.当state==0时,去唤醒后继节点
          protected boolean tryReleaseShared(int releases) {
                // Decrement count; signal when transition to zero
                for (;;) {
                    int c = getState();
                    if (c == 0)
                        return false;
                    int nextc = c-1;
                    if (compareAndSetState(c, nextc))
                        return nextc == 0;
                }
            }
        }
    

    await(long time,TimeUtil unit)

        public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
                throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
    //如果获取到锁返回true(state==0?1:-1)
    //否则进行入队,自旋获取锁,park等过程
            return tryAcquireShared(arg) >= 0 ||
                doAcquireSharedNanos(arg, nanosTimeout);
        }
    //添加了自旋阈值的控制spinForTimeoutThreshold
    private boolean doAcquireSharedNanos(int arg, long nanosTimeout)
                throws InterruptedException {
            if (nanosTimeout <= 0L)
                return false;
            final long deadline = System.nanoTime() + nanosTimeout;
            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 true;
                        }
                    }
                    nanosTimeout = deadline - System.nanoTime();
                    if (nanosTimeout <= 0L)
                        return false;
                    if (shouldParkAfterFailedAcquire(p, node) &&
                        nanosTimeout > spinForTimeoutThreshold)
                        LockSupport.parkNanos(this, nanosTimeout);
                    if (Thread.interrupted())
                        throw new InterruptedException();
                }
            } finally {
                if (failed)
                    cancelAcquire(node);
            }
        }
    

    调用await时
    共享锁获取失败(计数器还不为0),则将该线程封装为一个Node对象放入队列中,并阻塞该线程;
    共享锁获取成功(计数器为0),则从第一个节点开始依次唤醒后继节点,实现共享状态的传播。
    调用countDown时
    如果计数器不为0,则释放,继续阻塞,并把state的值减1;
    如果计数器为0,则唤醒节点,解除线程的阻塞状态。

    共享和独占的区别:
    共享锁在节点成为头节点获取到锁之后,立马唤醒后继节点,实现节点传播
    独占锁在获取锁且未释放锁之前,其他节点一致阻塞
    【参考博客】
    http://www.ideabuffer.cn/2017/03/19/深入理解AbstractQueuedSynchronizer(二)

    相关文章

      网友评论

          本文标题:AQS之CountDownLatch

          本文链接:https://www.haomeiwen.com/subject/skqciftx.html