-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchExample.java
More file actions
38 lines (31 loc) · 1.14 KB
/
CountDownLatchExample.java
File metadata and controls
38 lines (31 loc) · 1.14 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
package basic;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchExample {
static class Worker extends Thread{
private CountDownLatch countDownLatch;
public Worker(CountDownLatch countDownLatch,String name){
super(name);
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
System.out.println("Worker"+Thread.currentThread().getName()+" started");
try {
Thread.sleep(3000);
}catch (InterruptedException ex){
ex.printStackTrace();
}
System.out.println("Worker"+Thread.currentThread().getName()+" finished");
countDownLatch.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(2);
Worker A= new Worker(countDownLatch,"A");
Worker B= new Worker(countDownLatch,"B");
A.start();
B.start();
countDownLatch.await();
System.out.println("Main thread ended!");
}
}