当前位置:   article > 正文

concurrent-7-AQS-CountDownLatch,CyclicBarrier_aqs原理(执行过程源码, 队出队的细节,源码细节)、countdownlatch和cyclicba

aqs原理(执行过程源码, 队出队的细节,源码细节)、countdownlatch和cyclicbarrier的区别是什么源码级别

CountDownLatch

CountDownLatch#tryAcquireShared

   protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;   //获取状态是不是达到条件
        }
  • 1
  • 2
  • 3

CountDownLatch#tryReleaseShared

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))  //循环cas操作释放资源
                    return nextc == 0; //如果为true 需要唤醒等待的await节点
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

CyclicBarrier

CyclicBarrier#dowait

private int dowait(boolean timed, long nanos)
        throws InterruptedException, BrokenBarrierException,
               TimeoutException {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            final Generation g = generation;

            if (g.broken)
                throw new BrokenBarrierException();

            if (Thread.interrupted()) {
                breakBarrier();   //唤醒条件等待队列,设置broken = true  重置资源数量
                throw new InterruptedException();
            }

            int index = --count;    //如果为0,则开启栅栏
            if (index == 0) {  // tripped
                boolean ranAction = false;
                try {
                    final Runnable command = barrierCommand;
                    if (command != null)
                        command.run();   //执行栅栏开启后的方法
                    ranAction = true;
                    nextGeneration();   //开启下一个栅栏循环
                    return 0;
                } finally {
                    if (!ranAction)   //如果失败重置
                        breakBarrier();
                }
            }
            //如果index >0 
            // loop until tripped, broken, interrupted, or timed out
            for (;;) {
                try {
                    if (!timed)  //如果没有时间限制则一直等待被唤醒
                        trip.await();
                    else if (nanos > 0L)
                        nanos = trip.awaitNanos(nanos);
                } catch (InterruptedException ie) {
                    if (g == generation && ! g.broken) {
                        breakBarrier();
                        throw ie;
                    } else {
                        // We're about to finish waiting even if we had not
                        // been interrupted, so this interrupt is deemed to
                        // "belong" to subsequent execution.
                        Thread.currentThread().interrupt();
                    }
                }

                if (g.broken)
                    throw new BrokenBarrierException();

                if (g != generation)  //开启了下一个栅栏则return
                    return index;

                if (timed && nanos <= 0L) {  //超过了时间限制,则抛出异常
                    breakBarrier();
                    throw new TimeoutException();
                }
            }
        } finally {
            lock.unlock();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/148964
推荐阅读
相关标签
  

闽ICP备14008679号