forked from lihengming/java-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchTest.java
More file actions
32 lines (26 loc) · 907 Bytes
/
Copy pathCountDownLatchTest.java
File metadata and controls
32 lines (26 loc) · 907 Bytes
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
30
31
32
package juc;
import java.util.concurrent.CountDownLatch;
/**
* Created by 李恒名 on 2017/6/18.
*/
public class CountDownLatchTest {
static CountDownLatch latch = new CountDownLatch(3);//创建计数器并设置初始值为3
static void work() {
System.out.println(Thread.currentThread().getName() + " Work End");
latch.countDown();//计数器值-1
}
public static void main(String[] args) throws InterruptedException {
new Thread(() -> work()).start();
new Thread(() -> work()).start();
new Thread(() -> work()).start();
latch.await();//当前线程(主线程)等待计数器值为0,才会执行
System.out.println("Main Thread Work End");
/**
输出:
Thread-0 Work End
Thread-1 Work End
Thread-2 Work End
Main Thread Work End
*/
}
}