CompletableFuture 介绍

178 阅读5分钟

1. 文档介绍

www.runoob.com/manual/jdk1…

2. 使用目的

  • Runnable+Thread虽然提供了多线程的能力但是没有返回值
  • Callable+Thread的方法提供多线程和返回值的能力但是在获取返回值的时候会阻塞主线程

考虑以下场景:

  • 两个异步计算之间有依赖关系,第二个异步计算需要第一个异步计算的结果
  • 对于多个 task ,只要有一个任务返回了结果就返回结果

class CompletableFuture implements Future, CompletionStage

CompletionStage:完成阶段性接口,可以编排两个阶段,完成的顺序的相互关系

  • 在一个阶段完成后执行,如回调函数
  • 在一个阶段完成后执行,并且可以获取到执行的结果
  • 在一个阶段完成后执行,并且可以获取到执行的结果,并且可以根据结果生成新的结果,新结果类型可以是任意类型
  • 在现有阶段下添加一个新的阶段,在两个阶段同时完成时,执行
  • 在现有阶段下添加一个新的阶段,在两个阶段同时完成时,执行并且可以获取到执行的结果
  • 在现有阶段下添加一个新的阶段,在两个阶段同时完成时,执行并且可以获取到执行的结果,并且可以根据结果生成新的结果,新结果类型可以是任意类型
  • 在现有阶段下添加一个新的阶段,在两个阶段任意一个完成时,执行
  • 在现有阶段下添加一个新的阶段,在两个阶段任意一个完成时,执行并且可以获取到执行的结果
  • 在现有阶段下添加一个新的阶段,在两个阶段任意一个完成时,执行并且可以获取到执行的结果,并且可以根据结果生成新的结果,新结果类型可以是任意类型

CompletableFuture:

  • 新建一个带返回结果的 CompletableFuture
  • 新建一个不带返回结果的 CompletableFuture
  • 传入 n 个 CompletableFuture ,待所有 CompletableFuture 执行完成后执行回调
  • 传入 n 个 CompletableFuture ,待所有 CompletableFuture 任意一个执行完成后执行回调

3. api

3.1 CompletionStage

CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { return "execute CompletableFuture supplyAsync"; });

方法demo输出值含义
CompletionStage thenRun(Runnable action)completableFuture.thenRun(new Runnable() { @Override public void run() { System.out.println("completableFuture thenRun"); } });completableFuture thenRun执行完成后执行,不能获取返回结果,有异常不会经过
CompletionStage thenAccept(Consumer action)completableFuture.thenAccept(new Consumer() { @Override public void accept(String s) { System.out.println(s); } });execute CompletableFuture supplyAsync执行完成后执行,可以获取执行结果,有异常不会经过
CompletionStage thenApply(Function fn)completableFuture.thenApply(new Function<String, String>() { @Override public String apply(String s) { return s + " execute thenApply"; } }).thenAccept(new Consumer() { @Override public void accept(String s) { System.out.println(s); } });execute CompletableFuture supplyAsync execute thenApply执行完成后执行,可以获取执行结果,可以根据返回结果生成新的结果,有异常不会经过
CompletionStage runAfterBoth(CompletionStage other, Runnable action)completableFuture.runAfterBoth(CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { return 1; } }), new Runnable() { @Override public void run() { System.out.println("runAfterBoth"); } });runAfterBoth传入一个 CompletionStage 可以获取到两个阶段处理完成后执行,无法获取到结果,有异常不会经过
CompletionStage thenAcceptBoth (CompletionStage other, BiConsumer action)completableFuture.thenAcceptBoth(CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { return 1; } }), new BiConsumer<String, Integer>() { @Override public void accept(String s, Integer integer) { System.out.println(s); System.out.println(integer); } });execute CompletableFuture supplyAsync1传入一个 CompletionStage 可以获取到两个阶段处理完成后执行,可以获取到结果,有异常不会经过
CompletionStage thenCombine (CompletionStage other, BiFunction fn)completableFuture.thenCombine(CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { return 1; } }), new BiFunction<String, Integer, Boolean>() { @Override public Boolean apply(String s, Integer integer) { System.out.println(s); System.out.println(integer); return true; } }).thenAccept(new Consumer() { @Override public void accept(Boolean res) { System.out.println("thenCombine result = " + res); } });execute CompletableFuture supplyAsync1thenCombine result = true传入一个 CompletionStage 可以获取到两个阶段处理完成后执行,可以获取到结果,可以根据返回结果生成新的结果,有异常不会经过
CompletionStage acceptEither (CompletionStage other, Consumer action)completableFuture.runAfterEither(CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { return 1; } }), new Runnable() { @Override public void run() { System.out.println("runAfterEither"); } });runAfterEither传入一个 CompletionStage ,可以在任意一个阶段处理完成后执行,不能获取到结果,有异常不会经过
CompletionStage acceptEitherAsync (CompletionStage other, Consumer action)completableFuture.acceptEither(CompletableFuture.supplyAsync(new Supplier() { @Override public String get() { return "CompletableFuture2"; } }), new Consumer() { @Override public void accept(String s) { System.out.println(s); } });execute CompletableFuture supplyAsync或CompletableFuture2谁先执行完先获取谁的值传入一个 CompletionStage ,可以在任意一个阶段处理完成后执行,可以获取到结果,结果需要继承两个过程的类型,有异常不会经过
CompletionStage applyToEither (CompletionStage other, Function fn)completableFuture.applyToEither(CompletableFuture.supplyAsync(new Supplier() { @Override public String get() { return "CompletableFuture2"; } }), new Function<String, String>() { @Override public String apply(String s) { return s + " applyToEither"; } }).thenAccept(new Consumer() { @Override public void accept(String s) { System.out.println(s); } });(execute CompletableFuture supplyAsync或CompletableFuture2)+ applyToEither谁先执行完先获取谁的值传入一个 CompletionStage ,可以在任意一个阶段处理完成后执行,可以获取到结果,结果需要继承两个过程的类型,可以根据返回结果生成新的结果,有异常不会经过
CompletableFuture thenCompose( Function> fn)completableFuture.thenCompose(new Function<String, CompletionStage>() { @Override public CompletionStage apply(String s) { return CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { System.out.println(s); return 1; } }); } }).thenAccept(new Consumer() { @Override public void accept(Integer integer) { System.out.println(integer); } });execute CompletableFuture supplyAsync1可以根据返回的结果生成一个新的过程,有异常不会经过
CompletableFuture exceptionally( Function fn)completableFuture.exceptionally(new Function<Throwable, String>() { @Override public String apply(Throwable throwable) { return "exceptionally exeception"; } }).thenAccept(new Consumer() { @Override public void accept(String s) { System.out.println(s); } });有异常返回exceptionally exeception无异常返回execute CompletableFuture supplyAsync添加异常情况下返回值
CompletableFuture whenComplete( BiConsumer action)completableFuture.whenComplete(new BiConsumer<String, Throwable>() { @Override public void accept(String s, Throwable throwable) { System.out.println(s); System.out.println(throwable.getMessage()); } });completableFuture 中模拟 int a = 1 / 0;nulljava.lang.ArithmeticException: / by zero可以同时获取到返回值和异常信息
CompletableFuture handle( BiFunction fn)completableFuture.handle(new BiFunction<String, Throwable, Integer>() { @Override public Integer apply(String s, Throwable throwable) { return 1; } }).thenAccept(new Consumer() { @Override public void accept(Integer integer) { System.out.println(integer); } });1可以同时获取到返回值和异常信息,同时根据结果生成新的结果

4.2 CompletableFuture

方法demo输出值含义
static CompletableFuture allOf(CompletableFuture... cfs)CompletableFuture.allOf( CompletableFuture.supplyAsync(new Supplier() { @Override public String get() { TimeUnit.SECONDS.sleep(3); System.out.println("completableFuture1"); return "completableFuture"; } }), CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { TimeUnit.SECONDS.sleep(1); System.out.println("completableFuture2"); return 0; } })).thenAccept(new Consumer() { @Override public void accept(Void unused) { System.out.println("allOf"); } }); System.in.read();completableFuture2completableFuture1allOf所有的过程都执行完以后执行
static CompletableFuture anyOf(CompletableFuture... cfs)CompletableFuture.anyOf( CompletableFuture.supplyAsync(new Supplier() { @Override public String get() { TimeUnit.SECONDS.sleep(3); System.out.println("completableFuture1"); return "completableFuture"; } }), CompletableFuture.supplyAsync(new Supplier() { @Override public Integer get() { TimeUnit.SECONDS.sleep(1); System.out.println("completableFuture2"); return 0; } })).thenAccept(new Consumer() { @Override public void accept(Object o) { System.out.println("anyOf res = " + String.valueOf(o)); } }); System.in.read();completableFuture2anyOf res = 0completableFuture1任意一个过程执行完以后执行,并且可以获取到执行完成的结果
static CompletableFuture runAsync(Runnable runnable)返回一个新的CompletableFuture
static CompletableFuture supplyAsync(Supplier supplier)返回一个新的CompletableFuture,可以获取到返回结果
T getNow(T valueIfAbsent)如果已完成,则返回结果值(或抛出任何遇到的异常),否则返回给定的valueIfAbsent