Groovy里使用CountDownLatch
生活随笔
收集整理的這篇文章主要介紹了
Groovy里使用CountDownLatch
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Latch的字面意思:彈簧鎖
CountDownLatch是java.util.concurrent包里的一個同步工具類。
CountDownLatch的構(gòu)造函數(shù),接收一個類型為整型的參數(shù),代表CountDownLatch所在的線程,在執(zhí)行await方法后能夠返回,所需要在其他線程內(nèi)調(diào)用其countDown方法的次數(shù)。
測試代碼和打印輸出:
timer.schedule新啟動了一個線程,在新線程里調(diào)用countDown,而主線程執(zhí)行await進入阻塞狀態(tài),待新線程調(diào)用一次countDown之后,主線程立即從await方法的阻塞狀態(tài)中返回。
package jerry;import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnitCountDownLatch called = new CountDownLatch(1) println "main thread id: " + Thread.currentThread().getId();Timer timer = new Timer() timer.schedule(new TimerTask() {void run() {println "call countDown in another thread: " + Thread.currentThread().getId();called.countDown()} }, 220)println "before calling called.await in main thread: " + Thread.currentThread().getId(); called.await(10, TimeUnit.SECONDS) println "after calling called.await in main thread: " + Thread.currentThread().getId();要獲取更多Jerry的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":
總結(jié)
以上是生活随笔為你收集整理的Groovy里使用CountDownLatch的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim 中批量注释和批量条件删除(转)
- 下一篇: 【深度学习】医学图像分割损失函数简介