警告!!警告!!本文会剧透复联四!!还未观看过影片的同窗慎重。编程
今天来讲说CompletableFuture之灭霸的故事。bash
CompletableFuture
,它是Java1.8提供的用来支持异步编程的工具类。在Java1.5里就提供了Future接口。app
咱们知道Future
是强大的可是在多任务有聚合关系的状况下用起来就有点麻烦,而CompletableFuture
就是用来解决任务之间的聚合关系的。对于灭霸来讲他的得汇集六颗无限宝石,加上一个汇集宝石力量的手套,才能打响指!因此最终打响指这个任务依赖于汇集六颗宝石的任务和有手套。异步
话很少说先上代码,复联四的故事是这样的(因演示CompletableFuture须要,没有严格按照剧情)异步编程
CompletableFuture<String> infinityStones = CompletableFuture.supplyAsync(() -> "六颗无限宝石");
CompletableFuture<Void> mittens = CompletableFuture.runAsync(() -> {
System.out.println("用于集合六颗无限宝石力量来打响指的手套");
});
CompletableFuture<String> thanosSnap = mittens.thenCombine(infinityStones, (m, stons) -> {
System.out.println("获取" + stons);
System.out.println("把宝石放到手套上");
System.out.println("灭霸:我就是天命!(打响指)");
int a = 1 / 0; //钢铁侠自告奋勇,替换了手套;
return "呵呵寂寞,一群渣渣";
}).exceptionally(e ->{
System.out.println("钢铁侠抢夺了手套,举起手臂,轻轻打响指");
return "钢铁侠:i am iron man";
});
System.out.println(thanosSnap.join());
System.out.println("灭霸看着满天成灰的手下..心中流下了痛苦的泪水");
}
##输出的故事情节以下
用于集合六颗无限宝石力量来打响指的手套
获取六颗无限宝石
把宝石放到手套上
灭霸:我就是天命!(打响指)
钢铁侠抢夺了手套,举起手臂,轻轻打响指
钢铁侠:i am iron man
灭霸看着满天成灰的手下..心中流下了痛苦的泪水
复制代码
可怜的灭霸就这样离开了咱们,还有咱们的钢铁侠帅的一批。I love you three thousand times.
工具
我们仍是先回到CompletableFuture
上,解释一下上面的方法。CompletableFuture
主要是经过下面四个静态方法来建立的。post
public static CompletableFuture<Void> runAsync(Runnable runnable) //使用默认线程池(ForkJoinPool)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) //使用传入的线程池
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) //使用默认线程池(ForkJoinPool)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) //使用传入的线程池
复制代码
runAsync
和supplyAsync
的差异在于runAsync
方法不支持返回值而supplyAsync
支持。ui
这里还须要知道一个东西CompletionStage
,它能够认为你的异步计算中的一个阶段,经过它能够很清晰的描述你的任务流的各任务之间的时序关系。如串行关系,and关系,or关系。CompletableFuture
实现了CompletionStage
。spa
如下为CompletionStage接口中串行的方法,都是任务之间有依赖关系,必须一个任务执行完另外一个任务才能执行。有Async的话就是不在当前线程内执行。异步再起一个线程执行。线程
thenApply 能接收参数也支持返回值,也就是接收上一阶段的输出做为本阶段的输入
thenAccept 支持接收参数可是没有返回值。
thenRun 不接受参数也不支持返回值,名副其实,就是run。
thenCompose 容许对两个 CompletionStage 进行流水线操做。
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);
public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) ;
public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, Executor executor) ;
复制代码
如下为CompletionStage接口中and关系的方法,强调的不是顺序而是两个任务都完成了就行,没前后的要求。咱们上面代码用到的就是and关系。参数类型删掉了,就留个方法名有点印象就好了,具体仍是得本身用的时候看。几个方法的差异就是参数的不一样。
public <U,V> CompletionStage<V> thenCombine(other,fn);
public <U,V> CompletionStage<V> thenCombineAsync(other,fn);
public <U,V> CompletionStage<V> thenCombineAsync(other,fn,executor);
public <U> CompletionStage<Void> thenAcceptBoth(other,action);
public <U> CompletionStage<Void> thenAcceptBothAsync(other,action);
public <U> CompletionStage<Void> thenAcceptBothAsync(other,action, executor);
public CompletionStage<Void> runAfterBoth(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor);
复制代码
or的关系就是两个任务,里面只要有一个完成就执行下一阶段的。若是是有返回值的话,那就是谁先返回用谁的结果执行下一个阶段的任务。
public <U> CompletionStage<U> applyToEither(other fn);
public <U> CompletionStage<U> applyToEitherAsync(other,fn);
public <U> CompletionStage<U> applyToEitherAsync(other, fn,executor);
public CompletionStage<Void> acceptEither(other,action);
public CompletionStage<Void> acceptEitherAsync(other,action);
public CompletionStage<Void> acceptEitherAsync(other,action,executor);
public CompletionStage<Void> runAfterEither(other,action);
public CompletionStage<Void> runAfterEitherAsync(other,action);
public CompletionStage<Void> runAfterEitherAsync(other,action,executor);
复制代码
在上述的参数方法内是不能抛出可检查异常,可是运行有时候仍是会异常。CompletionStage 就提供了如下几种处理异常的方法。 whenComplete和handle就相似try catch中的finally(我姓方平行四边形的方),反正会执行到这个方法,就能够用它来处理异常,两个的差异就是whenComplete不支持返回结果,handle支持。
public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor)
public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,Executor executor);
public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
复制代码
若是是任务之间有聚合关系就能够用CompletableFuture来解决!Java帮你写好的工具类不用白不用!
若有错误欢迎指正! 我的公众号:yes的练级攻略