-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountDownLatchTest.java
36 lines (31 loc) · 1.1 KB
/
CountDownLatchTest.java
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
package com.algorithm.demo.thread;
import java.util.concurrent.CountDownLatch;
/**
* CountDownLatch: 一个线程(或者多个), 等待另外N个线程完成某个事情之后才能执行。一个线程中可以多次执行countDown
*/
public class CountDownLatchTest {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(5);
for(int i=0;i<5;i++){
new Thread(new readNum(i,countDownLatch)).start();
}
countDownLatch.await();
System.out.println("线程执行结束。。。。");
}
static class readNum implements Runnable{
private int id;
private CountDownLatch latch;
public readNum(int id,CountDownLatch latch){
this.id = id;
this.latch = latch;
}
@Override
public void run() {
synchronized (this){
System.out.println("id:"+id);
latch.countDown();
System.out.println("线程组任务"+id+"结束,其他任务继续");
}
}
}
}