concurrent-7-AQS-CountDownLatch,CyclicBarrier
来源:互联网 发布:js幻灯片轮播代码 编辑:程序博客网 时间:2024/06/03 17:37
CountDownLatch
CountDownLatch#tryAcquireShared
protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; //获取状态是不是达到条件 }
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节点 } }
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(); } }
阅读全文
0 0
- concurrent-7-AQS-CountDownLatch,CyclicBarrier
- java.util.concurrent.CyclicBarrier 与 java.util.concurrent.CountDownLatch
- java.util.concurrent包详解:CyclicBarrier和CountDownLatch
- Concurrent常用工具类的使用(CyclicBarrier/CountDownLatch/Callable)
- java.util.concurrent 下的Semaphore CyclicBarrier CountDownLatch 分析使用
- concurrent包下的CyclicBarrier和CountDownLatch以及Semaphore
- concurrent CountDownLatch
- Java多线程——java.util.concurrent库中的CyclicBarrier和CountDownLatch工具
- Java.utils.concurrent包中的几个有用的类(CountDownLatch、CyclicBarrier、Semaphore)
- concurrent-5-AQS-Condition
- concurrent-5-AQS-ReentrantReadWriteLock
- concurrent-6-AQS-Semaphore
- CyclicBarrier And CountDownLatch Tutorial
- CountDownLatch CyclicBarrier DelayQueue PriorityBlockingQueue
- Java CountDownLatch 和 CyclicBarrier
- CountDownLatch 与 CyclicBarrier应用
- CountDownLatch and CyclicBarrier
- CyclicBarrier and CountDownLatch
- office 2016优化
- 495. Teemo Attacking
- 视频编解码质量评价---BDBR与BD-PSNR
- 归并排序
- 开始学算法1
- concurrent-7-AQS-CountDownLatch,CyclicBarrier
- C语言:探测 unsigned int 的边界
- [洛谷P2375]动物园
- fclose函数的一个注意点
- 712. Minimum ASCII Delete Sum for Two Strings
- Educational Codeforces Round 30 837A. Chores
- [安卓]Android Studio 3.0 安装包说明(2017.10)
- 异常解决:Mapped Statements collection does not contain value for …
- 摆动排序 II-LintCode