激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java中多線程同步類 CountDownLatch

Java中多線程同步類 CountDownLatch

2020-09-22 10:15行者無疆-ITer Java教程

本篇文章主要介紹了Java中多線程同步類 CountDownLatch的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧

多線程開發(fā)中,常常遇到希望一組線程完成之后在執(zhí)行之后的操作,java提供了一個(gè)多線程同步輔助類,可以完成此類需求:

類中常見的方法:

Java中多線程同步類 CountDownLatch

其中構(gòu)造方法:

CountDownLatch(int count) 參數(shù)count是計(jì)數(shù)器,一般用要執(zhí)行線程的數(shù)量來賦值。

long getCount():獲得當(dāng)前計(jì)數(shù)器的值。

void countDown():當(dāng)計(jì)數(shù)器的值大于零時(shí),調(diào)用方法,計(jì)數(shù)器的數(shù)值減少1,當(dāng)計(jì)數(shù)器等數(shù)零時(shí),釋放所有的線程。

void await():調(diào)所該方法阻塞當(dāng)前主線程,直到計(jì)數(shù)器減少為零。

代碼例子:

線程類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.concurrent.CountDownLatch;
public class TestThread extends Thread{
CountDownLatch cd;
String threadName;
public TestThread(CountDownLatch cd,String threadName){
 this.cd=cd;
 this.threadName=threadName;
 
}
@Override
public void run() {
 System.out.println(threadName+" start working...");
 dowork();
 System.out.println(threadName+" end working and exit...");
 cd.countDown();//告訴同步類完成一個(gè)線程操作完成
 
}
private void dowork(){
 try {
 Thread.sleep(2000);
 System.out.println(threadName+" is working...");
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 
}
 
}

測(cè)試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.concurrent.CountDownLatch;
public class TsetCountDownLatch {
 
 public static void main(String[] args) {
 try {
  CountDownLatch cd = new CountDownLatch(3);// 表示一共有三個(gè)線程
  TestThread thread1 = new TestThread(cd, "thread1");
  TestThread thread2 = new TestThread(cd, "thread2");
  TestThread thread3 = new TestThread(cd, "thread3");
  thread1.start();
  thread2.start();
  thread3.start();
  cd.await();//等待所有線程完成
  System.out.println("All Thread finishd");
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
}

輸出結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
thread1 start working...
thread2 start working...
thread3 start working...
thread2 is working...
thread2 end working and exit...
thread1 is working...
thread3 is working...
thread3 end working and exit...
thread1 end working and exit...
All Thread finishd

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!

原文鏈接:http://www.cnblogs.com/ITer-jack/p/6791803.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 羞羞视频免费网站含羞草 | 91久久久久久亚洲精品禁果 | 九九热九九热 | 亚洲一区二区中文字幕在线观看 | 最新视频一区二区 | 九九黄色 | 性生活香蕉视频 | 色综合一区二区 | lutube成人福利在线观看 | 亚洲影视中文字幕 | 精品一区二区亚洲 | 蜜桃视频在线免费播放 | 伊人在线| 一区视频 | 久久久资源网 | 欧美日韩亚洲精品一区二区三区 | 久久99国产综合精品 | 免费一级在线视频 | 视频一区二区久久 | 在线小视频国产 | 久久2019中文字幕 | 玩偶姐姐 在线观看 | 国产精选电影免费在线观看网站 | 日本在线观看一区二区 | 国产日产精品一区二区三区四区 | 日本免费一区二区三区四区 | 本色视频aaaaaa一级网站 | 欧美一级特级 | 欧美国产第一页 | 中国国语毛片免费观看视频 | 国产精品久久久久久久久久10秀 | 久久国产精品免费视频 | 久久久久国产成人精品亚洲午夜 | 草久免费 | 中文字幕视频在线播放 | 精品国产一区二区三区天美传媒 | 视屏一区 | 久久精品二区 | 亚洲精品tv久久久久久久久久 | 成人综合区一区 | 人人舔人人射 |