溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何正確的使用CountDownLatch

發(fā)布時間:2021-06-15 13:58:01 來源:億速云 閱讀:660 作者:Leah 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關如何正確的使用CountDownLatch,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

public class Test {

	public static void main(String[] args) {
	   CountDownLatch begin = new CountDownLatch(1);
	   CountDownLatch end = new CountDownLatch(2);

	   for(int i=0; i<2; i++){
		   Thread thread = new Thread(new Player(begin,end));
		   thread.start();
	   }

	   try{
		   System.out.println("the race begin");
		   begin.countDown();
		   end.await();
		   System.out.println("the race end");
	   }catch(Exception e){
			e.printStackTrace();
	   }

	}
}


/**
 * 選手
 */
class Player implements Runnable{

	private CountDownLatch begin;

	private CountDownLatch end;

	Player(CountDownLatch begin,CountDownLatch end){
		this.begin = begin;
		this.end = end;
	}

	public void run() {

		try {
			begin.await();
			System.out.println(Thread.currentThread().getName() + " arrived !");;
			end.countDown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}

下面是運行結果

如何正確的使用CountDownLatch

可以看到 通過CountDownLatch 的使用 我們控制了線程的執(zhí)行順序。

在上面代碼中,我們使用到await()方法 和 countDown() 方法 。我們驗證一下它們各自的作用。

首先 驗證await() 方法。將main方法中的 end.await() 注釋掉,下面是注釋掉后的運行結果

如何正確的使用CountDownLatch

可以看到主線程沒有等待代表選手的線程結束,直接宣布比賽結束了!剛開始就結束的比賽- -

這里可以看出,await() 方法具有阻塞作用

其次 我們來驗證countDown方法,將代表選手線程中的 end.countDown() 進行注釋,下面是運行結果

如何正確的使用CountDownLatch

程序一直在運行,所有選手都已經到了終點,但是裁判就是不宣傳比賽結束,他在等什么呢?

我們猜測countDown() 方法具有喚醒阻塞線程的作用。

那我們也許會問,既然有喚醒阻塞線程的作用,那么我們只調用一次countDown() 方法不就是可以喚醒被阻塞的主線程了嗎?

我們試一下,取消上面coutDown()的注釋,再次創(chuàng)建一個選手,代碼如下

  class Player2 implements Runnable{

		private CountDownLatch begin;

		private CountDownLatch end;

		Player2(CountDownLatch begin,CountDownLatch end){
			this.begin = begin;
			this.end = end;
		}

		public void run() {

			try {
				begin.await();
				System.out.println(Thread.currentThread().getName() + " arrived !");
//                end.countDown();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

main 方法也修改如下,創(chuàng)建了兩個不同的選手

public static void main(String[] args)
	{
		CountDownLatch begin = new CountDownLatch(1);
		CountDownLatch end = new CountDownLatch(2);

		Thread thread = new Thread(new Player(begin, end));
		thread.start();

		Thread thread2 = new Thread(new Player2(begin, end));
		thread2.start();

		try
		{
			System.out.println("the race begin");
			begin.countDown();
			end.await();
			System.out.println("the race end");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

運行一下,下面是結果

如何正確的使用CountDownLatch

主程序一直阻塞,沒有被喚醒,裁判上廁所上得有點久啊!

這樣看來countDown() 并不是直接喚醒線程,有點像一個計數(shù)器,倒計時的那種。

查看API文檔,果然,我們在構造函數(shù)中添加了參數(shù)2,就需要調用 2 次 countDown() 才能將 end.await() 阻塞的線程喚醒。

CountDownLatch end = new CountDownLatch(2);

總結一下,

  1、CountDownLatch end = new CountDownLatch(N); //構造對象時候 需要傳入?yún)?shù)N

  2、end.await() 能夠阻塞線程 直到調用N次end.countDown() 方法才釋放線程

  3、end.countDown() 可以在多個線程中調用 計算調用次數(shù)是所有線程調用次數(shù)的總和

以上就是如何正確的使用CountDownLatch,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI