Skip to content

Commit 8268b57

Browse files
author
Bruce Eckel
committed
Separated into their own utility class
1 parent 5de72c0 commit 8268b57

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

concurrent/CompletableOperations.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,22 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.concurrent.*;
6+
import static onjava.CompletableUtilities.*;
67

78
public class CompletableOperations {
89
static CompletableFuture<Integer> cfi(int i) {
910
return
1011
CompletableFuture.completedFuture(
1112
new Integer(i));
1213
}
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-
}
3114
public static void main(String[] args) {
3215
showr(cfi(1)); // Basic test
3316
voidr(cfi(2).runAsync(() ->
3417
System.out.println("runAsync")));
3518
voidr(cfi(3).thenRunAsync(() ->
3619
System.out.println("thenRunAsync")));
20+
voidr(CompletableFuture.runAsync(() ->
21+
System.out.println("runAsync is static")));
3722
showr(CompletableFuture.supplyAsync(() -> 99));
3823
voidr(cfi(4).thenAcceptAsync(i ->
3924
System.out.println("thenAcceptAsync: " + i)));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// concurrent/CompletableUtilities.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package onjava;
6+
import java.util.concurrent.*;
7+
8+
public class CompletableUtilities {
9+
// Get and show value stored in a CF:
10+
public static void showr(CompletableFuture<?> c) {
11+
try {
12+
System.out.println(c.get());
13+
} catch(InterruptedException
14+
| ExecutionException e) {
15+
throw new RuntimeException(e);
16+
}
17+
}
18+
// For CF operations that have no value:
19+
public static void voidr(CompletableFuture<Void> c) {
20+
try {
21+
c.get(); // Returns void
22+
} catch(InterruptedException
23+
| ExecutionException e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)