JUC三大辅助类
java.util.concurrent包為我們提供了三大輔助類,幫助我們在某些場景下快速編寫并發代碼。
CountDownLatch(int count)
Constructs a CountDownLatch initialized with the given count.
構造一個用給定計數初始化的CountDownLatch。
CountDownLatch類可以設置一個計數器,然后通過countDown方法來進行減一的操作,使用await方法等待計數器不大于0,然后繼續執行await方法之后的語句。
CountDownLatch主要有兩個方法,當一個或多個線程調用await方法時,這些線程會阻塞。其他線程調用countDown方法會將計數器減一(調用countDown方法的線程不會阻塞)。
當計數器的值變為0時,因await方法而阻塞的線程會被喚醒,繼續執行。
案例演示:
import java.util.concurrent.CountDownLatch;// 演示CountDownLatch public class CountDownLatchDemo {// 6個同學陸續離開教室后,班長鎖門public static void main(String[] args) throws InterruptedException {// 創建CountDownLatch對象,設置初始值CountDownLatch countDownLatch = new CountDownLatch(6);// 6個同學陸續離開教室for (int i = 1; i <= 6; i++) {new Thread(() -> {System.out.println(Thread.currentThread().getName() + "號同學離開了教室");// 計數-1countDownLatch.countDown();}, String.valueOf(i)).start();}// 等待countDownLatch.await();System.out.println(Thread.currentThread().getName() + "班長鎖門");} }執行結果:
2. CyclicBarrier
CyclicBarrier(int parties)
Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and does not perform a predefined action when the barrier is tripped.
創建一個新的CyclicBarrier,當給定數量的參與方(線程)在等待它時,它將觸發,并且在觸發barrier時不執行預定義的操作。
CyclicBarrier(int parties, Runnable barrierAction)
Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and which will execute the given barrier action when the barrier is tripped, performed by the last thread entering the barrier.
創建一個新的CyclicBarrier,當給定數量的參與方(線程)在等待它時,它將被絆倒;當barrier被絆倒時,它將執行給定的barrier動作,由最后一個進入barrier的線程執行。
CyclicBarrier允許一組線程互相等待,直到到達某個公共屏障點。在涉及一組固定大小的線程的程序中,這些線程必須不時地互相等待,此時CyclicBarrier很有用。因為該barrier在釋放等待線程后可以重用,所以稱它為循環柵欄。
CyclicBarrier的構造方法第一個參數是目標障礙數,每次執行CyclicBarrier一次障礙數會加一,如果達到了目標障礙數,才會執行cyclicBarrier.await()之后的語句;可以將CyclicBarrier理解為加一操作。
案例演示:
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier;// 演示CyclicBarrier public class CyclicBarrierDemo {// 集齊7顆龍珠就可以召喚神龍private static final int NUMBER = 7;public static void main(String[] args) {// 創建CyclicBarrier對象CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, () -> {System.out.println("集齊7顆龍珠就可以召喚神龍");});// 集齊7顆龍珠的過程for (int i = 1; i <= NUMBER ; i++) {new Thread(() -> {System.out.println(Thread.currentThread().getName() + "星龍珠");// 等待try {cyclicBarrier.await();} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}}, String.valueOf(i)).start();}} }執行結果:
Semaphore(int permits)
Creates a Semaphore with the given number of permits and nonfair fairness setting.
使用給定的許可數量和非公平性設置創建一個信號量。
Semaphore(int permits, boolean fair)
Creates a Semaphore with the given number of permits and the given fairness setting.
使用給定的許可數量和公平性設置創建一個信號量。
一個計數信號量,從概念上講,信號量維護了一個許可集。如有必要,在許可可用前會阻塞每一個acquire方法,然后再獲取該許可。每個acquire方法添加一個許可,從而可能釋放一個正在阻塞的獲取者。但是,不使用實際的許可對象,semaphore只對可用許可的號碼進行計數,并采取相應的行動。
Semaphore通常用于限制可以訪問某些資源(物理或邏輯的)的線程數目。
案例演示:
import java.util.Random; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit;// 演示Semaphore public class SemaphoreDemo {// 6輛汽車,停3個停車位public static void main(String[] args) {// 創建Semaphore,設置許可數量Semaphore semaphore = new Semaphore(3);// 模擬6輛汽車for (int i = 1; i <= 6 ; i++) {new Thread(() -> {try {// 搶占semaphore.acquire();System.out.println(Thread.currentThread().getName() + " 搶到了車位");// 設置隨機停車時間TimeUnit.SECONDS.sleep(new Random().nextInt(5));System.out.println(Thread.currentThread().getName() + " -----離開了車位");} catch (InterruptedException e) {e.printStackTrace();} finally {// 釋放semaphore.release();}}, String.valueOf(i)).start();}} }執行結果:
總結
- 上一篇: 电脑进程说明,常见,作用,说明,是否,查
- 下一篇: 并发编程-Semaphore,Cycli