Java CountDownLatch與CyclicBarrier及Semaphore使用教程
CountDownLatch
CountDownLatch是一個(gè)倒數(shù)的計(jì)數(shù)器閥門,初始化時(shí)閥門關(guān)閉,指定計(jì)數(shù)的數(shù)量,當(dāng)數(shù)量倒數(shù)減到0時(shí)閥門打開,被阻塞線程被喚醒。
public class CountDownLatchDemo {
public static void main(String[] args) throws InterruptedException {
// 總數(shù)是6,必須要執(zhí)行任務(wù)的時(shí)候,再使用 !
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 1; i <=6 ; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName()+" Go out");
countDownLatch.countDown(); // 數(shù)量-1
},String.valueOf(i)).start();
}
countDownLatch.await(); // 等待計(jì)數(shù)器歸零,然后再向下執(zhí)行
System.out.println("Close Door");
}
}
原理
countDownLatch.countDown();//數(shù)量-1
countDownLatch.await();//等待
每次有線程調(diào)用 countDown() 數(shù)量-1,假設(shè)計(jì)數(shù)器變?yōu)?,countDownLatch.await() 就會(huì)被喚醒,繼續(xù)執(zhí)行!
CyclicBarrier
CyclicBarrier是一個(gè)可循環(huán)的屏障,它允許多個(gè)線程在執(zhí)行完相應(yīng)的操作后彼此等待共同到達(dá)一個(gè)point,等所有線程都到達(dá)后再繼續(xù)執(zhí)行。
CyclicBarrier也可以像CountDownLatch一樣適用于多個(gè)子任務(wù)并發(fā)執(zhí)行,當(dāng)所有子任務(wù)都執(zhí)行完后再繼續(xù)接下來的工作。
public class CyclicBarrierDemo {
public static void main(String[] args) {
// 召喚龍珠的線程
CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
System.out.println("召喚神龍成功!");
});
for (int i = 1; i <=7 ; i++) {
final int temp = i;
// lambda能操作到 i 嗎
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"收集"+temp+"個(gè)龍珠");
try {
cyclicBarrier.await(); // 等待
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
}
}
Semaphore
Semaphore翻譯過來是信號(hào)量的意思,它的作用是控制多個(gè)線程對同一個(gè)資源的訪問線程數(shù)量。比如在停車場停車,里面有10個(gè)車位,當(dāng)這10個(gè)車位被停滿的時(shí)候其他的車只能等待(堵塞)里面有車駛出(release)然后再進(jìn)入。
public class SemaphoreDemo {
public static void main(String[] args) {
// 線程數(shù)量:停車位! 限流!
Semaphore semaphore = new Semaphore(3);
for (int i = 1; i <=6 ; i++) {
new Thread(()->{
// acquire() 得到
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName()+"搶到車位");
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"離開車位");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release(); // release() 釋放
}
},String.valueOf(i)).start();
}
}
}
semaphore.acquire();獲得,假設(shè)如果已經(jīng)滿了,等待,等待被釋放為止!
semaphore.release();釋放,會(huì)將當(dāng)前的信號(hào)量釋放 + 1,然后喚醒等待的線程!
作用: 多個(gè)共享資源互斥的使用!并發(fā)限流,控制最大的線程數(shù)!
到此這篇關(guān)于Java CountDownLatch與CyclicBarrier及Semaphore使用教程的文章就介紹到這了,更多相關(guān)Java CountDownLatch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java并發(fā)編程之CountDownLatch的使用
CountDownLatch是一個(gè)倒數(shù)的同步器,常用來讓一個(gè)線程等待其他N個(gè)線程執(zhí)行完成再繼續(xù)向下執(zhí)行,本文主要介紹了CountDownLatch的具體使用方法,感興趣的可以了解一下2023-05-05
springboot如何實(shí)現(xiàn)自動(dòng)裝配源碼解讀
這篇文章主要介紹了springboot如何實(shí)現(xiàn)自動(dòng)裝配源碼賞析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
java的Map集合中按value值進(jìn)行排序輸出的實(shí)例代碼
下面小編就為大家?guī)硪黄猨ava的Map集合中按value值進(jìn)行排序輸出的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
springboot動(dòng)態(tài)定時(shí)任務(wù)的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于springboot動(dòng)態(tài)定時(shí)任務(wù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

