Created
December 7, 2015 02:41
-
-
Save cho45/6b3a1f714f0716537f58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.function.Consumer; | |
public class Main { | |
public static void main(String[] args) { | |
new Main().doMain(args); | |
} | |
private static <T> CompletableFuture<T> async(Consumer<CompletableFuture<T>> function) { | |
final CompletableFuture<T> future = new CompletableFuture<>(); | |
function.accept(future); | |
return future; | |
} | |
public void doMain(String[] args) { | |
final CompletableFuture<Void> future1 = async(f -> { | |
try { | |
f.complete("Foobar"); | |
} catch (Exception e) { | |
f.completeExceptionally(e); | |
} | |
}).thenAccept(x -> { | |
System.out.println(x); | |
}); | |
final CompletableFuture<Void> future2 = async(f -> { | |
try { | |
throw new RuntimeException("exception"); | |
} catch (Exception e) { | |
f.completeExceptionally(e); | |
} | |
}).exceptionally(ex -> { | |
System.out.println("exception!!"); | |
return ex; | |
}).thenAccept(x -> { | |
System.out.println(x); | |
}); | |
CompletableFuture.allOf(future1, future2).thenRun(() -> { | |
System.out.println("All done"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment