-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchTest.java
More file actions
44 lines (40 loc) · 1.2 KB
/
CountDownLatchTest.java
File metadata and controls
44 lines (40 loc) · 1.2 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
package com.sychronize;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* CountDownLatch测试
* @author 李
*
*/
public class CountDownLatchTest implements Runnable{
public static CountDownLatch count = new CountDownLatch(10);
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " -> start");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
count.countDown();
}
System.out.println(Thread.currentThread().getName() + " -> end");
}
public static void main(String[] args) {
CountDownLatchTest countDownLatchTest = new CountDownLatchTest();
List<Thread> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Thread t1 = new Thread(countDownLatchTest);
list.add(t1);
}
list.forEach(o->o.start());
try {
count.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main 线程 结束");
}
}