-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletableFutureExample.java
More file actions
55 lines (44 loc) · 1.48 KB
/
CompletableFutureExample.java
File metadata and controls
55 lines (44 loc) · 1.48 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
55
package challenge1.java8;
import externalLegacyCodeNotUnderOurControl.PriceService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static externalLegacyCodeNotUnderOurControl.PrintlnWithThreadname.println;
import static java.util.concurrent.CompletableFuture.supplyAsync;
/**
* This example uses CompletableFuture which is available since Java 8.
*
* @author Marcus Fihlon, www.fihlon.ch
*/
public class CompletableFutureExample {
private static final int NUMBER_OF_SERVICE_CALLS = 10;
private final ExecutorService pool;
private int price;
private int count;
public static void main(final String... args) {
new CompletableFutureExample().run();
}
private CompletableFutureExample() {
this.pool = Executors.newFixedThreadPool(3);
this.price = 0;
this.count = 0;
}
/**
* Create the price services asynchronously.
*/
private void run() {
for (int i = 0; i < NUMBER_OF_SERVICE_CALLS; i++) {
supplyAsync(() -> new PriceService().getPrice(), this.pool)
.thenAcceptAsync(this::collector);
}
}
/**
* Collect the answers and calculate the average price.
*/
private synchronized void collector(final int price) {
this.price += price;
if (++this.count == NUMBER_OF_SERVICE_CALLS) {
println("The average price is: " + this.price / this.count);
System.exit(0);
}
}
}