MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / CompletablePizza

Class CompletablePizza

concurrent/CompletablePizza.java:10–43  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import onjava.Timer;
9
10public class CompletablePizza {
11 static final int QUANTITY = 5;
12 public static CompletableFuture<Pizza>
13 makeCF(Pizza za) {
14 return CompletableFuture
15 .completedFuture(za)
16 .thenApplyAsync(Pizza::roll)
17 .thenApplyAsync(Pizza::sauce)
18 .thenApplyAsync(Pizza::cheese)
19 .thenApplyAsync(Pizza::toppings)
20 .thenApplyAsync(Pizza::bake)
21 .thenApplyAsync(Pizza::slice)
22 .thenApplyAsync(Pizza::box);
23 }
24 public static void
25 show(CompletableFuture<Pizza> cf) {
26 try {
27 System.out.println(cf.get());
28 } catch(Exception e) {
29 throw new RuntimeException(e);
30 }
31 }
32 public static void main(String[] args) {
33 Timer timer = new Timer();
34 List<CompletableFuture<Pizza>> pizzas =
35 IntStream.range(0, QUANTITY)
36 .mapToObj(Pizza::new)
37 .map(CompletablePizza::makeCF)
38 .collect(Collectors.toList());
39 System.out.println(timer.duration());
40 pizzas.forEach(CompletablePizza::show);
41 System.out.println(timer.duration());
42 }
43}
44/* Output:
4598
46Pizza 1: ROLLED

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected