Skip to content

Commit 82ea1ec

Browse files
committed
CountDownLatch同步工具类
1 parent f4393a8 commit 82ea1ec

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

thread/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
## [Condition](src/main/java/com/cpucode/java/condition)
140140

141141
- [x] [Condition](src/main/java/com/cpucode/java/condition/ConditionDemo.java)
142+
- [x] [CountDownLatch同步工具类](src/main/java/com/cpucode/java/condition/CountDownLatchDemo.java)
142143

143144
- [返回文件目录](#文件目录)
144145

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.cpucode.java.condition;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
/**
6+
* @author : cpucode
7+
* @date : 2021/7/30
8+
* @time : 15:59
9+
* @github : https://github.com/CPU-Code
10+
* @csdn : https://blog.csdn.net/qq_44226094
11+
*/
12+
public class CountDownLatchDemo extends Thread{
13+
public static void main(String[] args) throws InterruptedException {
14+
CountDownLatch countDownLatch = new CountDownLatch(3);
15+
16+
new Thread(()-> {
17+
System.out.println("" + Thread.currentThread().getName() + "-执行中");
18+
countDownLatch.countDown();
19+
20+
System.out.println(""+Thread.currentThread().getName()+"-执行完毕");
21+
},"t1").start();
22+
23+
new Thread(()->{
24+
System.out.println(""+Thread.currentThread().getName()+"-执行中");
25+
countDownLatch.countDown();
26+
System.out.println(""+Thread.currentThread().getName()+"-执行完毕");
27+
},"t2").start();
28+
29+
new Thread(()->{
30+
System.out.println(""+Thread.currentThread().getName()+"-执行中");
31+
countDownLatch.countDown();
32+
System.out.println(""+Thread.currentThread().getName()+"-执行完毕");
33+
},"t3").start();
34+
35+
countDownLatch.await();
36+
System.out.println("所有线程执行完毕");
37+
}
38+
}

0 commit comments

Comments
 (0)