Java中的CountDownLatch
對於併發執行,**Java中的CountDownLatch**是一個重要的類,它確保一個或多個執行緒排隊等待其他執行緒完成其操作集。
為了更好地理解Java中的CountDownLatch,在本文中,您將學習CountDownLatch的工作原理,並透過示例和方法來學習CountDownLatch。
Java中的CountDownLatch及其工作流程
基於計數值,CountDownLatch用於多種用途,如下所示:
當我們以計數值1開始CountDownLatch時,它將簡單地充當開/關閂鎖或門。
另一方面,當我們以計數值N開始CountDownLatch時,它會導致一個執行緒等待,直到N個執行緒完成某些操作或某個操作已完成N次。
Java中的CountDownLatch的工作原理
在建立CountDownLatch物件期間,我們應該定義它必須等待的執行緒數。一旦指定,所有這些執行緒都需要透過呼叫CountDownLatch.countDown()來進行倒計時。達到零時,佇列中的任務開始執行。
示例
這是一個說明Java中CountdownLatch的Java程式:
import java.util.concurrent.CountDownLatch; public class CountDownLatchDemo{ public static void main(String args[]) throws InterruptedException{ // Lets create a task that needs to wait for four threads before it begins CountDownLatch latch = new CountDownLatch(4); // Let's create four employee threads and begin them. Employee first = new Employee(1000, latch,"EMPLOYEE-1"); Employee second = new Employee(2000, latch,"EMPLOYEE-2"); Employee third = new Employee(3000, latch,"EMPLOYEE-3"); Employee fourth = new Employee(4000, latch,"EMPLOYEE-4"); first.start(); second.start(); third.start(); fourth.start(); // The main task waits for four threads latch.await(); // Main thread has started System.out.println(Thread.currentThread().getName() +" has finished"); } } // A class to represent threads for which the main thread waits. class Employee extends Thread{ private int delay; private CountDownLatch latch; public Employee(int delay, CountDownLatch latch,String name){ super(name); this.delay = delay; this.latch = latch; } @Override public void run(){ try{ Thread.sleep(delay); latch.countDown(); System.out.println(Thread.currentThread().getName()+ " finished"); } catch (InterruptedException e){ e.printStackTrace(); } } }
輸出
EMPLOYEE-1 finished EMPLOYEE-2 finished EMPLOYEE-3 finished EMPLOYEE-4 finished main has finished
從上面的程式中,我們可以理解它從我們傳遞給建構函式的計數開始。每當呼叫**countDown()**方法時,**await**方法都會停止,直到計數達到0。在此之後,所有等待的執行緒都將被釋放,並且對await的任何後續呼叫都會立即返回。
理解CountDownLatch的建構函式
CountDownLatch的建構函式是引數化的,它只接受一個整數計數作為值。它構造CountDownLatch並以給定的計數值開始。
**注意**:每當計數的值為負數時,建構函式都會丟擲IllegalArgumentException異常。
語法
public CountDownLatch( int count )
引數
count -> 表示線上程能夠透過await()之前,必須呼叫countdown()的次數。
CountDownLatch的方法
CountDownLatch類中有多種方法可用於併發控制。此外,CountDownLatch類還繼承了java.lang.Object類的其他方法。
java.lang.Object類的方法如下:
clone
equals
finalize
getClass
hashCode
notify
notifyAll
wait
wait
wait
CountDownLatch提供以下方法:
1. await()
await()方法會將當前執行緒掛起,直到以下任一情況發生:
閂鎖已倒計時至零。
執行緒未中斷。
如果當前計數值為零,則await()方法會立即返回。
只要當前計數既不是零也不是負數,await()方法就會停用當前執行緒的排程,並且執行緒會保持休眠狀態,直到發生以下情況之一:
由於呼叫countDown(),計數值達到零。
另一個執行緒中斷當前執行緒。
語法
以下是await()方法的語法:
public void await()
它不接受或返回任何值。
丟擲異常
噹噹前執行緒在等待時被中斷時,await()方法會**丟擲**InterruptedException異常。
2. await(long timeout, TimeUnit unit)
這是await()方法的另一個變體,它使當前執行緒等待,直到以下任一情況發生:
閂鎖已倒計時至零。
指定的等待時間已完成。
執行緒中斷。
噹噹前計數值為零時,await()方法立即返回true。
只要當前計數既不是零也不是負數,await()方法就會停用當前執行緒的排程,並且執行緒會保持休眠狀態,直到發生以下情況之一:
由於呼叫countDown(),計數值達到零。
指定的等待時間結束。
當前執行緒被其他執行緒中斷。
語法
public boolean await( long timeout, TimeUnit unit)
語法中使用的引數
**timeout** - 這是一個長整型引數,指定最大等待時間。
**unit** - 這指定了timeout引數的時間單位。
3. 返回值
當計數達到零時,返回true;當在計數達到零之前等待時間已完成時,返回false。
4. 丟擲異常
當等待期間當前執行緒中斷時,await()方法會丟擲InterruptedException異常。
5. countDown
CountDownLatch類提供的另一個重要方法是countDown()方法。它記錄所有閂鎖計數,並在計數達到零時釋放所有等待的執行緒。它執行以下操作:
噹噹前計數大於零時,計數遞減。
當新的計數為零時,所有等待的執行緒都將重新啟用以進行執行緒排程。
但是,如果當前計數等於零,則不會發生任何事情。
6. getCount
getCount()是CountDownLatch類提供的另一個重要方法。它用於獲取當前使用的閂鎖的計數。
語法
public long getCount()
它不接受任何引數,而返回閂鎖的當前計數。
7. toString
CountDownLatch類提供的最後一個方法是toString()方法。它用於獲取一個字串,該字串將確定閂鎖及其狀態。
語法
public String toString()
8. 重寫
它重寫了Object類中的toString方法。
9. 返回值
它返回一個字串,該字串將確定閂鎖及其狀態。
總結
至此,我們已經完成了CountDownLatch教程。我們希望您現在已經瞭解了CountDownLatch是什麼以及它的工作原理。我們還向您展示了示例和程式碼以供參考。如果您覺得這篇文章有幫助,請點贊。