-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDownLatchExample.java
More file actions
47 lines (37 loc) · 1.11 KB
/
Copy pathCountDownLatchExample.java
File metadata and controls
47 lines (37 loc) · 1.11 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
package com.stacktips.threads;
import java.util.concurrent.CountDownLatch;
class Task implements Runnable {
String service;
CountDownLatch latch;
public Task(String service, CountDownLatch latch) {
this.service = service;
this.latch = latch;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(service + " is up");
latch.countDown();
}
}
public class CountDownLatchExample {
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(3);
Thread t1 = new Thread(new Task("Service1", latch));
Thread t2 = new Thread(new Task("Service2", latch));
Thread t3 = new Thread(new Task("Service3", latch));
t1.start();
t2.start();
t3.start();
try {
latch.await();
System.out.println("All services are up, Starting Main Application now");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}