-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchDemo.java
More file actions
54 lines (40 loc) · 1.61 KB
/
CountDownLatchDemo.java
File metadata and controls
54 lines (40 loc) · 1.61 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
45
46
47
48
49
50
51
52
53
54
package kb.concurrent.synchronizers;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class DependentService implements Runnable {
private CountDownLatch latch;
private String serviceId;
public DependentService(String serviceId, CountDownLatch latch) {
this.latch = latch;
this.serviceId = serviceId;
}
@Override
public void run() {
// Simulate some initialization logic e. g. Data Base connection
System.out.println(serviceId + " is initialized.");
latch.countDown();
// Here the service may continue with other operation
}
}
public class CountDownLatchDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(4);
// count the number of times {@link #countDown} must be invoked before threads
// can pass through {@link #await}
CountDownLatch latch = new CountDownLatch(3);
executor.submit(new DependentService("Service_1", latch));
executor.submit(new DependentService("Service_2", latch));
executor.submit(new DependentService("Service_3", latch));
// With `await()` we ensures that all services have been initialized.
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// The code here will be executed after all services initialization.
System.out.println("All dependent services are initialized.");
executor.shutdown();
}
}