|
3 | 3 | // We make no guarantees that this code is fit for any purpose. |
4 | 4 | // Visit http://OnJava8.com for more book information. |
5 | 5 | import java.util.concurrent.*; |
| 6 | +import static onjava.CompletableUtilities.*; |
6 | 7 |
|
7 | 8 | public class CompletableOperations { |
8 | 9 | static CompletableFuture<Integer> cfi(int i) { |
9 | 10 | return |
10 | 11 | CompletableFuture.completedFuture( |
11 | 12 | new Integer(i)); |
12 | 13 | } |
13 | | - // Get and show value stored in a CF: |
14 | | - static void showr(CompletableFuture<Integer> c) { |
15 | | - try { |
16 | | - System.out.println(c.get()); |
17 | | - } catch(InterruptedException |
18 | | - | ExecutionException e) { |
19 | | - throw new RuntimeException(e); |
20 | | - } |
21 | | - } |
22 | | - // For CF operations that have no value: |
23 | | - static void voidr(CompletableFuture<Void> c) { |
24 | | - try { |
25 | | - c.get(); // Returns void |
26 | | - } catch(InterruptedException |
27 | | - | ExecutionException e) { |
28 | | - throw new RuntimeException(e); |
29 | | - } |
30 | | - } |
31 | 14 | public static void main(String[] args) { |
32 | 15 | showr(cfi(1)); // Basic test |
33 | 16 | voidr(cfi(2).runAsync(() -> |
34 | 17 | System.out.println("runAsync"))); |
35 | 18 | voidr(cfi(3).thenRunAsync(() -> |
36 | 19 | System.out.println("thenRunAsync"))); |
| 20 | + voidr(CompletableFuture.runAsync(() -> |
| 21 | + System.out.println("runAsync is static"))); |
37 | 22 | showr(CompletableFuture.supplyAsync(() -> 99)); |
38 | 23 | voidr(cfi(4).thenAcceptAsync(i -> |
39 | 24 | System.out.println("thenAcceptAsync: " + i))); |
|
0 commit comments