您的位置:首页 > 健康 > 养生 > Java线程池异步CompletableFuture,创建一步对象、回调方法、线程串行化、两任务组合、多任务组合

Java线程池异步CompletableFuture,创建一步对象、回调方法、线程串行化、两任务组合、多任务组合

2024/12/27 6:50:17 来源:https://blog.csdn.net/z1299236067/article/details/141936894  浏览:    关键词:Java线程池异步CompletableFuture,创建一步对象、回调方法、线程串行化、两任务组合、多任务组合

        JDK1.8后引入的 CompletableFuture 类,提供了一种更为便捷的异步编程方式,可以轻松地进行异步操作的组合和串联。

        CompletableFuture实现了Future类,可以获取到异步结果。

创建异步对象

conpletableFuture提供了四个静态方法来创建一个异步的操作。

static CompletableFuture<Void> runAsync(Runnable runnable)

static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)

static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)

static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

1.runXxx都没有返回结果,supplyXxx都是可以获取返回结果

2.可以传入自定义线程池,否则默认线程池。 

默认线程池为ForkJoinPool

简单事例

// 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {System.out.printf("当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.printf("运行结果" + i);  return i;}, executorService);System.out.printf("\n启动结束----"+integerCompletableFuture.get());}

 回调方法

方法完成后的感知

CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)
CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)
CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)

带Async意思,前面执行成功后,我们还要干一件事情,交给线程池处理。

不带Async ,前后使用同一个线程处理。

方法不以Async结尾的,意味着Action使用相同的线程执行,而Async可能会使用其他线程执行(如果使用相同的线程池,也可能会被同一个线程执行) 

whenComplete可以处理正常和异常的计算结果,exceptionally处理异常情况

简单事例

// 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {System.out.printf("\n当前线程:" + Thread.currentThread().getId());int i = 10 / 0;System.out.printf("\n运行结果" + i);return i;}, executorService).whenCompleteAsync((t,u)->{System.out.printf("\n异步任务完成,结果是:"+t+"异常是"+u);//whenCompleteAsync 可以得到异常信息,无法修改返回数据}).exceptionally(throwable -> {// 可以感知异常,同时返回默认值return 10;});System.out.printf("\n启动结束----"+integerCompletableFuture.get());}

handle方法

方法执行完成后的处理

与Complete一样,可对结果做最后处理(可处理异常),可改变返回值

<U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)
<U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)
<U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor)

简单事例

// 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {System.out.printf("\n当前线程:" + Thread.currentThread().getId());int i = 10 / 0;System.out.printf("\n运行结果" + i);return i;}, executorService).handle((res,thr)->{if (res !=null){return res+1;}if(thr!=null){return 0;}return res;});System.out.printf("\n启动结束----"+integerCompletableFuture.get());}

线程串行化方法

//不接收上一个执行的结果

CompletableFuture<Void> thenRun(Runnable action )

CompletableFuture<Void> thenRunAsync(Runnable action)

CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)

// 接收上一个执行的结果

CompletableFuture<Void> thenAccept(Consumer<? super T> action)

CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)

CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)

//接受上一个执行结果,处理并把处理结果返回 

<U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)

<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)

<U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)

thenRun方法:只要上面任务执行完成,就开始执行thenRun(无法感知到上一个执行结果)。

thenAccept方法:消费处理结果(接收到上一个任务的处理结果),接受任务的处理结果,并消费处理,无返回结果。

thenApply方法:当一个线程依赖另一个线程时,获取上一个任务返回的结果,并返回当前任务的返回值。

简单事例

// 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {// thenRun:不能获取上一步的执行结果CompletableFuture<Void> thenRunFuture = CompletableFuture.supplyAsync(() -> {System.out.printf("\n当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.printf("\n运行结果" + i);return i;}, executorService).thenRunAsync(() -> {System.out.printf("\n2启动开始----");});// thenAccept能接收上一步接口,没有返回值CompletableFuture.supplyAsync(() -> {System.out.printf("\n当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.printf("\n运行结果" + i);return i;}, executorService).thenAcceptAsync(res->{System.out.printf("\n2启动开始----"+res);});//thenApply能接收上一步接口,有返回值CompletableFuture<Integer> thenApplyFuture = CompletableFuture.supplyAsync(() -> {System.out.printf("\n当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.printf("\n运行结果" + i);return i;}, executorService).thenApplyAsync((t) -> {System.out.printf("\n2启动开始----" + t);return t + 2;});System.out.printf("\n结束:"+thenApplyFuture.get());}

两任务组合-都要完成

CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)

CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)

CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)

<U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)

<U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action)

<U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor)

<U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)

<U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)

<U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor)

runAfterBoth:组合两个future,不需要获取future的结果,只需两个future处理完任务后,处理该任务。

thenAcceptBoth:组合两个future,获取两个future任务的返回结果,然后处理任务,没有返回值。

thenCombine:组合两个future,获取两个future任务的返回结果,然后处理任务,返回当前任务的返回值。

简单事例

// 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {
// 任务1CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {System.out.printf("\n任务1线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.printf("\n任务1结果" + i);return i;}, executorService);
//任务2CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {System.out.printf("\n任务2线程:" + Thread.currentThread().getId());System.out.printf("\n任务2结果");return "Hello";}, executorService);// runAfterBothAsync 不能感知前两步结果future1.runAfterBothAsync(future2,()->{System.out.printf("\n任务3");},executorService);// thenAcceptBothAsync 能拿到前两步的结果future1.thenAcceptBothAsync(future2,(f1,f2)->{System.out.printf("\n任务3f1:"+f1);System.out.printf("\n任务3f2:"+f2);},executorService);// stringCompletableFuture 能拿到前两步的结果并返回结果CompletableFuture<String> stringCompletableFuture = future1.thenCombineAsync(future2, (f1, f2) -> {System.out.printf("\n任务3f1:" + f1);System.out.printf("\n任务3f2:" + f2);return f1 + f2;}, executorService);System.out.printf("----"+stringCompletableFuture.get());}

两任务组合-一个完成

两个任务中,任意一个future完成,执行任务。 

CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)

CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)

CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action,  Executor executor)

CompletableFuture<Void> acceptEither( CompletionStage<? extends T> other, Consumer<? super T> action)

CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)

CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)

<U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn)

<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn)

<U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, Executor executor)

 runAfterEither:两个任务有一个执行完成,不获取返回值。

 acceptEither:两个任务有一个执行完成,获取返回值,没有新的返回值。

 applyToEither:两个任务有一个执行完成,获取返回值,处理任务并有新的返回值。

简单事例

 // 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {System.out.printf("\n任务1线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.printf("\n任务1结果" + i);return i;}, executorService);CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {System.out.printf("\n任务2线程:" + Thread.currentThread().getId());try {Thread.sleep(3000);System.out.printf("\n任务2结果");} catch (InterruptedException e) {e.printStackTrace();}return "Hello";}, executorService);// runAfterEitherAsync 不感知结果,没有返回值。future1.runAfterEitherAsync(future2,()->{System.out.printf("\n任务3");},executorService);// acceptEitherAsync 感知结果,没有返回值。future1.acceptEitherAsync(future2,(res)->{System.out.printf("\n任务3:"+res);},executorService);// applyToEitherAsync 感知结果,有返回值。CompletableFuture<String> stringCompletableFuture = future1.applyToEitherAsync(future2, (res) -> {System.out.printf("\n任务3:" + res);return res.toString() + "aaaa";}, executorService);System.out.printf("最终结果"+stringCompletableFuture.get());}

多任务组合

static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)

static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) 

allOf:等待所有任务完成。

anyOf:只要有一个任务完成。

简单事例

// 创建线程池static ExecutorService executorService = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {System.out.printf("\n查询商品名称");return "手机";},executorService);CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {System.out.printf("\n查询商品颜色");return "黑色";},executorService);CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {try {Thread.sleep(3000);System.out.printf("\n查询商品品牌");} catch (InterruptedException e) {e.printStackTrace();}return "华为";},executorService);CompletableFuture<Void> future = CompletableFuture.allOf(future1, future2, future3);// 等待所有结果全部完成,如果不加future.get();,顺序是乱的future.get();System.out.printf("\nallOf完成");System.out.printf("\nallOf完成"+future3.get()+future1.get()+future2.get());CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2, future3);System.out.printf("\nanyOf完成");System.out.printf("\nanyOf完成"+anyOf.get());}

如果对您有帮助辛苦帮忙点赞收藏!!! 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com