如何正確的使用CountDownLatch

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

成都創(chuàng)新互聯(lián)公司長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為濱江企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),濱江網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

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();
		}

	}
}

下面是運(yùn)行結(jié)果

如何正確的使用CountDownLatch

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

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

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

如何正確的使用CountDownLatch

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

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

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

如何正確的使用CountDownLatch

程序一直在運(yùn)行,所有選手都已經(jīng)到了終點(diǎn),但是裁判就是不宣傳比賽結(jié)束,他在等什么呢?

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

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

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

  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)建了兩個(gè)不同的選手

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();
		}

	}

運(yùn)行一下,下面是結(jié)果

如何正確的使用CountDownLatch

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

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

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

CountDownLatch end = new CountDownLatch(2);

總結(jié)一下,

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

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

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

以上就是如何正確的使用CountDownLatch,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:如何正確的使用CountDownLatch
瀏覽路徑:http://bm7419.com/article38/gegpsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化ChatGPT、外貿(mào)網(wǎng)站建設(shè)、用戶體驗(yàn)、虛擬主機(jī)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)