touch-ups

This commit is contained in:
Bruce Eckel 2017-01-02 14:22:25 -08:00
parent 8268b577c9
commit 45d4e5d85b
4 changed files with 12 additions and 12 deletions

View File

@ -14,16 +14,16 @@ public class Batter {
new Nap(100); new Nap(100);
return ingredient; return ingredient;
} }
static <T> CompletableFuture<T> cf(T ingredient) { static <T> CompletableFuture<T> prep(T ingredient) {
return CompletableFuture return CompletableFuture
.completedFuture(ingredient) .completedFuture(ingredient)
.thenApply(Batter::prepare); .thenApplyAsync(Batter::prepare);
} }
public static CompletableFuture<Batter> mix() { public static CompletableFuture<Batter> mix() {
CompletableFuture<Eggs> eggs = cf(new Eggs()); CompletableFuture<Eggs> eggs = prep(new Eggs());
CompletableFuture<Milk> milk = cf(new Milk()); CompletableFuture<Milk> milk = prep(new Milk());
CompletableFuture<Sugar> sugar = cf(new Sugar()); CompletableFuture<Sugar> sugar = prep(new Sugar());
CompletableFuture<Flour> flour = cf(new Flour()); CompletableFuture<Flour> flour = prep(new Flour());
CompletableFuture CompletableFuture
.allOf(eggs, milk, sugar, flour) .allOf(eggs, milk, sugar, flour)
.join(); .join();

View File

@ -7,8 +7,8 @@ import onjava.Nap;
public class CatchCompletableExceptions { public class CatchCompletableExceptions {
static void handleException(int failcount) { static void handleException(int failcount) {
// Call Function only if there's an exception, // Call the Function only if there's an
// Must produce same type as came in: // exception, must produce same type as came in:
CompletableExceptions CompletableExceptions
.test("exceptionally", failcount) .test("exceptionally", failcount)
.exceptionally((ex) -> { // Function .exceptionally((ex) -> { // Function

View File

@ -7,8 +7,8 @@ import java.util.concurrent.*;
public class CompletedMachina { public class CompletedMachina {
public static void main(String[] args) { public static void main(String[] args) {
CompletableFuture<Machina> cf = CompletableFuture<Machina> cf =
CompletableFuture.completedFuture( CompletableFuture.completedFuture(
new Machina(0)); new Machina(0));
try { try {
Machina m = cf.get(); // Doesn't block Machina m = cf.get(); // Doesn't block
} catch(InterruptedException | } catch(InterruptedException |

View File

@ -26,7 +26,7 @@ public class FrostedCake {
.thenCombineAsync(Frosting.make(), .thenCombineAsync(Frosting.make(),
(cake, frosting) -> (cake, frosting) ->
new FrostedCake(cake, frosting)) new FrostedCake(cake, frosting))
.thenAcceptAsync(System.out::println).join()); .thenAcceptAsync(System.out::println)
// Need to rewrite... .join());
} }
} }