實(shí)例講解Java并發(fā)編程之閉鎖
閉鎖相當(dāng)于一扇門(mén),在閉鎖到達(dá)結(jié)束狀態(tài)之前,這扇門(mén)一直是關(guān)閉著的,沒(méi)有任何線程可以通過(guò),當(dāng)?shù)竭_(dá)結(jié)束狀態(tài)時(shí),這扇門(mén)才會(huì)打開(kāi)并容許所有線程通過(guò)。它可以使一個(gè)或多個(gè)線程等待一組事件發(fā)生。閉鎖狀態(tài)包括一個(gè)計(jì)數(shù)器,初始化為一個(gè)正式,正數(shù)表示需要等待的事件數(shù)量。countDown方法遞減計(jì)數(shù)器,表示一個(gè)事件已經(jīng)發(fā)生,而await方法等待計(jì)數(shù)器到達(dá)0,表示等待的事件已經(jīng)發(fā)生。CountDownLatch強(qiáng)調(diào)的是一個(gè)線程(或多個(gè))需要等待另外的n個(gè)線程干完某件事情之后才能繼續(xù)執(zhí)行。
場(chǎng)景應(yīng)用:
10個(gè)運(yùn)動(dòng)員準(zhǔn)備賽跑,他們等待裁判一聲令下就開(kāi)始同時(shí)跑,當(dāng)最后一個(gè)人通過(guò)終點(diǎn)的時(shí)候,比賽結(jié)束。10個(gè)運(yùn)動(dòng)相當(dāng)于10個(gè)線程,這里關(guān)鍵是控制10個(gè)線程同時(shí)跑起來(lái),還有怎么判斷最后一個(gè)線程到達(dá)終點(diǎn)??梢杂?個(gè)閉鎖,第一個(gè)閉鎖用來(lái)控制10個(gè)線程等待裁判的命令,第二個(gè)閉鎖控制比賽結(jié)束。
import java.util.concurrent.CountDownLatch;
class Aworker implements Runnable {
private int num;
private CountDownLatch begin;
private CountDownLatch end;
public Aworker(int num, final CountDownLatch begin, final CountDownLatch end) {
this.num = num;
this.begin = begin;
this.end = end;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println(num + "th people is ready");
begin.await(); //準(zhǔn)備就緒
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
end.countDown(); //計(jì)數(shù)器減一,到達(dá)終點(diǎn)
System.out.println(num + "th people arrive");
}
}
}
public class Race {
public static void main(String[] args) {
int num = 10;
CountDownLatch begin = new CountDownLatch(1);
CountDownLatch end = new CountDownLatch(num);
for (int i = 1; i <= num; i++) {
new Thread(new Aworker(i, begin, end)).start();
}
try {
Thread.sleep((long) (Math.random() * 5000));
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("judge say : run !");
begin.countDown(); //裁判一聲令下開(kāi)始跑
long startTime = System.nanoTime();
try {
end.await(); //等待結(jié)束
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
long endTime = System.nanoTime();
System.out.println("judge say : all arrived !");
System.out.println("spend time: " + (endTime - startTime));
}
}
}
輸出
1th people is ready 2th people is ready 4th people is ready 6th people is ready 3th people is ready 10th people is ready 8th people is ready 5th people is ready 7th people is ready 9th people is ready judge say : run ! 1th people arrive 4th people arrive 10th people arrive 5th people arrive 2th people arrive judge say : all arrived ! 9th people arrive 7th people arrive 8th people arrive 3th people arrive 6th people arrive spend time: 970933
相關(guān)文章
SpringBoot與SpringSecurity整合方法附源碼
這篇文章主要介紹了SpringBoot與SpringSecurity整合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
java實(shí)現(xiàn)簡(jiǎn)單的汽車(chē)租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的汽車(chē)租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
SpringBoot解決Required?String?parameter?xxx?is?not?prese
這篇文章主要介紹了SpringBoot解決Required?String?parameter?xxx?is?not?present問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot 普通類(lèi)調(diào)用Bean對(duì)象的一種方式推薦
這篇文章主要介紹了SpringBoot 普通類(lèi)調(diào)用Bean對(duì)象的一種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot多環(huán)境打包與配置文件排除實(shí)踐記錄
本文介紹了SpringBoot項(xiàng)目多環(huán)境打包與配置文件排除實(shí)踐,包括多環(huán)境配置的實(shí)現(xiàn)方法、打包時(shí)排除配置文件的方法以及動(dòng)態(tài)加載外部配置文件的最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-11-11

