1不见、CountDownLatch程序計(jì)數(shù)器
官方解釋?zhuān)?br>
A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
允許一個(gè)或多個(gè)線程等待澳化,直到其他線程的操作完成,再執(zhí)行稳吮。
2缎谷、CyclicBarrier循環(huán)屏障
官方解釋?zhuān)?br>
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.
一組線程全都等待,直到每個(gè)線程都到達(dá)一個(gè)公共的屏障點(diǎn)灶似,屏障才打開(kāi)列林,繼續(xù)后面的操作。
3喻奥、join
若在A線程執(zhí)行過(guò)程中席纽,執(zhí)行線程B的join方法,則A線程會(huì)等待撞蚕,直到B線程結(jié)束润梯。
4、awaitTermination
①shutdown() 線程池狀態(tài)置為shutdown,并不會(huì)立即停止:
停止接受外部submit的任務(wù)
內(nèi)部正在執(zhí)行或者隊(duì)列中的任務(wù)會(huì)繼續(xù)執(zhí)行甥厦,直到結(jié)束
②shutdownNow() 線程池狀態(tài)置為stop纺铭,試圖立即停止,事實(shí)上并不一定
停止接受外部submit的任務(wù)
忽略隊(duì)列中等待的任務(wù)
嘗試將正在跑的任務(wù)interrupt中斷
返回未執(zhí)行的任務(wù)列表
③awaitTermination(long timeOut, TimeUnit unit) 阻塞當(dāng)前線程
等待所有正在執(zhí)行或隊(duì)列中的任務(wù)執(zhí)行完
等待時(shí)間超時(shí)
線程被中斷刀疙,拋出InterruptedException
shutdown請(qǐng)求后所有任務(wù)執(zhí)行完畢舶赔,則返回true,超時(shí)谦秧,則返回false
通常與shutdown聯(lián)合使用
總結(jié):優(yōu)雅的關(guān)閉:shutdown
立即關(guān)閉竟纳,并返回未執(zhí)行任務(wù)列表:shutdownNow
設(shè)置超時(shí)時(shí)間,檢查線程池是否關(guān)閉:awaitTermination