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