CompletableFuture
- Future 可能被明确完成(设置它的值和状态),并可能被用作一个CompletionStage,支持依赖的功能和行动,在它完成时触发。
- 当两个或多个线程试图完成、completeException或取消CompletableFuture时,只有一个线程成功。除了这些和直接操作状态和结果的相关方法之外,CompletableFuture还通过以下策略实现接口CompletionStage.
- 为非异步方法的依赖完成提供的操作可以由完成当前CompletableFuture的线程执行,或者由完成方法的任何其他调用者执行。
- 所有没有显式Executor参数的异步方法都使用==ForkJoinPool.commonPool()==执行(除非它不支持至少两个并行级别,在这种情况下,会创建一个新线程来运行每个任务)。为了简化监视、调试和跟踪,所有生成的异步任务都是标记接口CompletableFuture.AsynchronousCompletionTask的实例。
- 所有CompletionStage方法都是独立于其他公共方法实现的,因此一个方法的行为不会受到子类中其他方法重写的影响。
CompletableFuture也实施了以下政策:
- 因为(不像FutureTask)这个类不能直接控制导致它完成的计算,所以取消被视为另一种形式的异常完成。方法cancel与completeexception (new CancellationException())具有相同的效果。方法iscompletedexception()可用于确定CompletableFuture是否以任何异常方式完成。
- 在使用CompletionException完成异常的情况下,方法get()和get(long, TimeUnit)抛出一个ExecutionException,其原因与在相应的CompletionException中持有的原因相同。为了简化大多数上下文中的使用,这个类还定义了join()和getNow(T)方法,在这些情况下直接抛出CompletionException。
supplyAsync(带有返回值)
- 返回一个新的CompletableFuture,该future由运行在ForkJoinPool.commonPool()中的任务异步完成,其值通过调用给定的供应商获得。
- 返回一个新的CompletableFuture,该future由在给定执行程序中运行的任务异步完成,其值通过调用给定供应商获得
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier){}
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
Executor executor){}
runAsync
- 返回一个新的CompletableFuture,该future在运行给定操作后由运行在ForkJoinPool.commonPool()中的任务异步完成。
- 返回一个新的CompletableFuture,该任务在运行给定的执行程序后异步完成。
public static CompletableFuture<Void> runAsync(Runnable runnable);
public static CompletableFuture<Void> runAsync(Runnable runnable,
Executor executor);
runAsync 和 supplyAsync
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("异步执行 1");
return 20;
}, executor);
future.get(); // 可以获取返回结果
计算结果完成时的回调方法
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 CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)
// runAsync 执行完结果之后,继续执行 whenComplete 中的 accept
// aVoid 表示 第一 runAsync 没有返回值
// Throwable 表示 runAsync 抛出的异常
CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
System.out.println("异步执行 1");
}, executor).whenComplete(new BiConsumer<Void, Throwable>() {
@Override
public void accept(Void aVoid, Throwable throwable) {
System.out.println("什么也没有输出");
}
});
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("异步执行 1");
return 20;
}, executor).whenComplete(new BiConsumer<Integer, Throwable>() {
@Override
public void accept(Integer aVoid, Throwable throwable) {
System.out.println("什么也没有输出");
}
});
thenApply 方法
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)
当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("异步执行 1");
try {
int num = 20 / 0;
} catch (Exception e) {
}
return 20;
}, executor).thenApply(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
System.out.println("获取了第一个任务的返回值,");
return integer + 29;
}
});
handle 方法
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);
handle 是执行任务完成时对结果的处理。
handle 方法和 thenApply 方法处理方式基本一样。不同的是 handle 是在任务完成后再执行,还可以处理异常的任务。thenApply 只可以执行正常的任务,任务出现异常则不执行 thenApply 方法。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("异步执行 1");
return 20;
}, executor).handle(new BiFunction<Integer, Throwable, Integer>() {
@Override
public Integer apply(Integer integer, Throwable throwable) {
System.out.println("zhixingksjkfsjdkfjskdf" + integer);
return integer + 30;
}
});
thenAccept 消费处理结果
接收任务的处理结果,并消费处理,无返回结果。
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);
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
return 20;
}, executor).thenAccept(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println();
}
});
thenRun 方法
跟 thenAccept 方法不一样的是,不关心任务的处理结果。只要上面的任务执行完成,就开始执行 thenAccept 。
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
return 20;
}, executor).thenRun(()->{
System.out.println("执行");
})
thenCombine 合并任务
thenCombine 会把 两个 CompletionStage 的任务都执行完成后,把两个任务的结果一块交给 thenCombine 来处理。
// 将 两个任务进行合并
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return 20;
}
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return 20;
}
}, executor);
CompletableFuture<Object> objectCompletableFuture = future1.thenCombine(future2, new BiFunction<Integer, Integer, Object>() {
@Override
public Object apply(Integer t, Integer u) {
return t + u;
}
});
System.out.println(objectCompletableFuture.get());
thenAcceptBoth
当两个CompletionStage都执行完成后,把结果一块交给thenAcceptBoth来进行消耗
public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action, Executor executor);
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return 20;
}
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
return 20;
}
}, executor);
CompletableFuture<Void> voidCompletableFuture = future1.thenAcceptBoth(future2, new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer integer, Integer integer2) {
System.out.println(integer + "\t" + integer2);
}
});
applyToEither 方法
两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的转化操作。
public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 30;
}
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 20;
}
}, executor);
CompletableFuture<Integer> result = future1.applyToEither(future2, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer;
}
});
System.out.println(result.get()); // 30
acceptEither
两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的消耗操作。
public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action,Executor executor);
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 30;
}
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 20;
}
}, executor);
CompletableFuture<?> result = future1.acceptEither(future2, new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println(integer);
}
});
runAfterEither 方法
两个CompletionStage,任何一个完成了都会执行下一步的操作(Runnable)
public CompletionStage<Void> runAfterEither(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action,Executor executor);
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 30;
}
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 20;
}
}, executor);
CompletableFuture<?> result = future1.runAfterEither(future2, ()-> {
System.out.println("任意一个执行完成都可以执行其他");
});
runAfterBoth
两个CompletionStage,都完成了计算才会执行下一步的操作(Runnable)
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);
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("future1");
return 30;
}
}, executor);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("future2");
return 20;
}
}, executor);
CompletableFuture<?> result = future1.runAfterBoth(future2, ()-> {
System.out.println("任意一个执行完成都可以执行其他");
});
thenCompose
thenCompose 方法允许你对两个 CompletionStage 进行流水线操作,第一个操作完成时,将其结果作为参数传递给第二个操作。
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) ;