java多線程JUC常用輔助類詳解
1.countDownLatch
減法計數(shù)器:實現(xiàn)調(diào)用幾次線程后,在觸發(fā)另一個任務(wù)
簡單代碼實現(xiàn):
舉例說明:就像五個人在同一房間里,有一個看門的大爺,當五個人都出去后,他才能鎖門,也就是說 執(zhí)行5次出門這個動作的線程后,才出發(fā)了鎖門的這個動作
import java.util.concurrent.CountDownLatch; /** * @program: juc * @description * @author: 不會編程的派大星 * @create: 2021-04-24 16:55 **/ public class CountDownLatchTest { public static void main(String[] args) throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(5); System.out.println("door is open"); for (int i = 1; i <= 5 ; i++) { new Thread(() -> { System.out.println(Thread.currentThread().getName()+" is going out"); countDownLatch.countDown(); },String.valueOf(i)).start(); } countDownLatch.await(); System.out.println("door is closed"); } }
代碼運行結(jié)果:
基本原理:
countDownLatch.countDown(); // 數(shù)量-1
countDownLatch.await(); // 等待計數(shù)器歸零,然后再向下執(zhí)行
每次有線程調(diào)用 countDown() 數(shù)量-1,假設(shè)計數(shù)器變?yōu)?,countDownLatch.await() 就會被喚醒,繼續(xù)執(zhí)行!
2.CyclicBarrier
這里我們簡單理解為 加法計數(shù)器
簡單代碼實現(xiàn):
舉例說明:這里只要集齊7顆龍珠,就執(zhí)行 打印 “7顆龍珠集齊了”的線程,這里我們先設(shè)置線程計數(shù)為8,看看會不會執(zhí)行打印的線程,然后在執(zhí)行計數(shù)為7的情況
1.cyclicBarrier計數(shù)為8的時候,執(zhí)行線程數(shù)量為7的時候:
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * @program: juc * @description * @author: 不會編程的派大星 * @create: 2021-04-24 17:31 **/ public class CyclicBarrierTest { public static void main(String[] args) { CyclicBarrier cyclicBarrier = new CyclicBarrier(8,new myThread()); for (int i = 1; i <= 7 ; i++) { int finalI = i; new Thread(() -> { System.out.println(Thread.currentThread().getName()+"收集了"+ finalI+"顆龍珠"); try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } },String.valueOf(i)).start(); } } } class myThread implements Runnable{ @Override public void run() { System.out.println("7顆龍珠集齊啦"); } }
執(zhí)行結(jié)果:
2.cyclicBarrier計數(shù)為1的時候,執(zhí)行線程數(shù)量為7的時候:
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; /** * @program: juc * @description * @author: 不會編程的派大星 * @create: 2021-04-24 17:31 **/ public class CyclicBarrierTest { public static void main(String[] args) { CyclicBarrier cyclicBarrier = new CyclicBarrier(7,new myThread()); for (int i = 1; i <= 7 ; i++) { int finalI = i; new Thread(() -> { System.out.println(Thread.currentThread().getName()+"收集了"+ finalI+"顆龍珠"); try { cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } },String.valueOf(i)).start(); } } } class myThread implements Runnable{ @Override public void run() { System.out.println("7顆龍珠集齊啦"); } }
執(zhí)行結(jié)果:
可以看到
CyclicBarrier cyclicBarrier = new CyclicBarrier(7,new myThread());
當執(zhí)行完7個線程后,才會執(zhí)行一個實現(xiàn)了Runnable接口的線程
3.Semaphore
簡單代碼實現(xiàn):
舉例說明:搶車位 ,6個車,最多同時只能有三個車進
import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; /** * @program: juc * @description * @author: 不會編程的派大星 * @create: 2021-04-24 18:26 **/ public class SemaphoreTest { public static void main(String[] args) { //線程數(shù)量 ,停車位,,6輛車在等,最多只能同時進來三個 限流 Semaphore semaphore = new Semaphore(3); for (int i = 1; i <= 6 ; i++) { new Thread(() -> { try { semaphore.acquire();//獲得 System.out.println(Thread.currentThread().getName()+"搶到車位了"); TimeUnit.SECONDS.sleep(3); System.out.println(Thread.currentThread().getName()+"離開車位了"); } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); //釋放 } },String.valueOf(i)).start(); } } }
運行結(jié)果:
原理說明:
semaphore.acquire()
; 獲得,假設(shè)如果已經(jīng)滿了,等待,等待被釋放為止!
semaphore.release()
; 釋放,會將當前的信號量釋放 + 1,然后喚醒等待的線程!
作用: 多個共享資源互斥的使用!并發(fā)限流,控制最大的線程數(shù)!
以上就是java多線程JUC常用輔助類詳解的詳細內(nèi)容,更多關(guān)于java多線程JUC輔助類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java?Spring?boot日期和時間統(tǒng)一設(shè)置三種方法
時間和日期的統(tǒng)一設(shè)置在項目中經(jīng)常是會遇到的,下面這篇文章主要給大家介紹了關(guān)于Java?Spring?boot日期和時間統(tǒng)一設(shè)置的三種方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-08-08springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析
這篇文章主要介紹了springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計算機應用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下2018-08-08