|
11 | 11 | */
|
12 | 12 | public class CompletableFutureAdditions {
|
13 | 13 | public static void main(String[] args) {
|
14 |
| - CompletableFutureAdditions additions = new CompletableFutureAdditions(); |
| 14 | + CompletableFutureAdditions cfAdditions = new CompletableFutureAdditions(); |
15 | 15 |
|
16 | 16 | System.out.println("Before First CompletableFuture " + currentTimeMillis());
|
17 |
| - CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> additions.sleep(2)); |
| 17 | + CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> cfAdditions.sleep(2)); |
18 | 18 | System.out.printf("Original Completable Future is done ? %b%n%n", cf.isDone());
|
19 | 19 |
|
20 |
| - System.out.println("Before a copy of Completable Future " + currentTimeMillis()); |
| 20 | + System.out.println("Before creating a copy of Completable Future " + currentTimeMillis()); |
21 | 21 | CompletableFuture<String> newCopy = cf.copy();
|
22 |
| - System.out.printf("Copy of Completable Future is done ? %b%n%n", cf.isDone()); |
23 |
| - newCopy.completeExceptionally(new InterruptedException("newCopy :: Some message")); |
| 22 | + System.out.printf("Copy of Completable Future is done ? %b%n%n", newCopy.isDone()); |
| 23 | + |
| 24 | + |
| 25 | + System.out.println("Before a copy of Completable Future for completing exceptionally " + currentTimeMillis()); |
| 26 | + CompletableFuture<String> newCopyForCompleteExceptionally = cf.copy(); |
| 27 | + System.out.printf("Copy of Completable Future for completing exceptionally is done ? %b%n%n", newCopyForCompleteExceptionally.isDone()); |
| 28 | + newCopyForCompleteExceptionally.completeExceptionally(new InterruptedException("newCopyForCompleteExceptionally :: Some message")); |
24 | 29 |
|
25 | 30 | System.out.println("Before Copying and completing on timeout " + currentTimeMillis());
|
26 | 31 | CompletableFuture<String> completeOnTimeOut = cf.copy().completeOnTimeout("completeOnTimeOut :: Failed to complete", 1, TimeUnit.SECONDS);
|
27 |
| - System.out.printf("Completable Future Complete on Timeout is done ? %b%n%n", cf.isDone()); |
| 32 | + System.out.printf("Completable future complete on timeout is done ? %b%n%n", completeOnTimeOut.isDone()); |
28 | 33 |
|
29 |
| - cf.thenAccept(s -> System.out.printf("Result after Original Future Completion : %s%n%n", s)); |
| 34 | + cf.thenAccept(s -> System.out.printf("%nResult after Original future completion : %s%n%n", s)); |
| 35 | + |
| 36 | + newCopy.thenAccept(s -> System.out.printf("%nResult after Original future completion : %s%n%n", s)); |
30 | 37 |
|
31 |
| - newCopy.thenAccept(s -> System.out.printf("Result after Copied Future Completion : %s%n%n", s)); |
| 38 | + newCopyForCompleteExceptionally.thenAccept(s -> System.out.printf("%nResult after Copied future Completion : %s%n%n", s)); |
32 | 39 |
|
33 |
| - completeOnTimeOut.thenAccept(s -> System.out.printf("Result after Copied and Timed out Future Completion : %s%n%n", s)); |
| 40 | + completeOnTimeOut.thenAccept(s -> System.out.printf("%nResult after Copied and timed out future Completion : %s%n%n", s)); |
34 | 41 |
|
35 |
| - System.out.println("Before Final Sleep " + currentTimeMillis()); |
36 |
| - additions.sleep(5); |
| 42 | + System.out.println("Before Final Sleep -- this is needed otherwise we will see no output. " + currentTimeMillis()); |
| 43 | + cfAdditions.sleep(5); |
37 | 44 |
|
38 | 45 | }
|
39 | 46 |
|
40 | 47 |
|
41 | 48 | private String sleep(int sleepTimeSeconds) {
|
| 49 | + System.out.println(Thread.currentThread().getId() + " is sleeping ... for " + sleepTimeSeconds); |
42 | 50 | try {
|
43 | 51 | Thread.sleep(TimeUnit.SECONDS.toMillis(sleepTimeSeconds));
|
44 | 52 | } catch (InterruptedException e) {
|
45 | 53 | throw new RuntimeException(e);
|
46 | 54 | }
|
| 55 | + System.out.println("Waking up! ... after " + sleepTimeSeconds); |
47 | 56 | return "Done : " + currentTimeMillis() + " By " + Thread.currentThread().getName();
|
48 | 57 | }
|
49 | 58 |
|
|
0 commit comments