多執行緒 CountDownLatch

2021-10-04 23:33:08 字數 1370 閱讀 8716

countdownlatch:

初始化執行緒池

run 執行任務,finally裡進行cdl.countdown()

future.get時進行超時處理,超時後將對應任務取消 並計數減1

@postconstruct

public

void

init()

public

boolean

execute()

list

futures =

newarraylist

<

>

(tasklist.

size()

);countdownlatch cdl =

newcountdownlatch

(tasklist.

size()

);for(

int i =

0; i < tasklist.

size()

;++i)

for(

int i =

0; i < futures.

size()

;++i)

catch

(timeoutexception e)

, e);if

(!future.

isdone()

)}} cdl.

await()

; log.

info

("本次任務{}條,耗時:{}ms"

, tasklist.

size()

, system.

currenttimemillis()

- starttime);}

catch

(exception e)

return

true;}

/** * description getnewhandletask 獲取可執行的執行緒

* @param cdl : 鎖

* @param task : 任務物件

* @return :

*/public handletask getnewhandletask

(countdownlatch cdl,t task)

/** * 任務執行緒

*/public

class

handletask

implements

runnable

@override

public

void

run(

)catch

(exception e)

finally

}}

java多執行緒之CountDownLatch

countdownlatch是乙個同步工具類,用來協調多個執行緒之 間的同步。其能夠使 乙個執行緒在等待另外一些執行緒完成各自工作之後,再繼續執行。使用乙個計數器進行實現。計數器初始值為執行緒的數量。當每乙個執行緒完成自己任務後,計數器的值就會減一。當計數器的值為0時,表示所有的執行緒都已經完成了任...

多執行緒之倒計時器CountDownLatch及原理

這個工具通常用來控制線程等待,它可以讓某乙個執行緒等待直到倒計時結束再開始執行。countdownlatch是 共享模式 的。構造方法 countdownlatch int count count就是計數的次數 主要方法 void await 讓當前執行緒等待,直到倒計時結束 long getcou...

多執行緒 多執行緒原理

我們首先要知道什麼是多執行緒,說白了就是多個執行緒,執行緒是什麼呢,其實就是程序執行的途徑,那麼說道這裡我們又引入了乙個新的名字,就是程序,那麼我們來看看什麼是程序,其實我們自己也能看到,啟動電腦的任務管理器,我們就可以看到程序選項,裡面是我們電腦所有的程序,我們會發現有很多的程序.簡單地說就是程序...