-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathUsingLatches.java
44 lines (40 loc) · 1.15 KB
/
UsingLatches.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
37
38
39
40
41
42
43
44
package br.com.leonardoz.features.synchronizers;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Latches are used to delay the progress of threads until it reach a terminal
* state
*
* Most common implementation is CountDownLatch.
*
* In CountDownLatch, each event adds 1. When ready, countDown() is called,
* decrementing by counter by 1. await() method releases when counter is 0.
*
* Single use synchronizer.
*/
public class UsingLatches {
public static void main(String[] args) {
var executor = Executors.newCachedThreadPool();
var latch = new CountDownLatch(3);
Runnable r = () -> {
try {
Thread.sleep(1000);
System.out.println("Service in " + Thread.currentThread().getName() + " initialized.");
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
executor.execute(r);
executor.execute(r);
executor.execute(r);
try {
latch.await(2, TimeUnit.SECONDS);
System.out.println("All services up and running!");
} catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdown();
}
}