CountDownLatch.countDown
生活随笔
收集整理的這篇文章主要介紹了
CountDownLatch.countDown
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于線程被await方法阻塞了,所以只有等到countdown方法使得state=0的時候才會被喚醒,我們來看看countdown做了什么
1. 只有當 state 減為 0 的時候,tryReleaseShared 才返回 true, 否則只是簡單的 state = state - 1
2. 如果state=0, 則調用doReleaseShared?喚醒處于await狀態下的線程
public final boolean releaseShared(int arg) { if (tryReleaseShared(arg)) { doReleaseShared(); return true; } return false; }用自旋的方法實現 state 減 1
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; } }?
總結
以上是生活随笔為你收集整理的CountDownLatch.countDown的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: acquireSharedInterru
- 下一篇: AQS.doReleaseShared