写点什么

Java8 异步编程 -CompletableFuture,孔浩 java 视频百度云盘

用户头像
极客good
关注
发布于: 刚刚

System.out.println("thread2:" + f.get());} catch (InterruptedException | ExecutionException e) {e.printStackTrace();}}).start();


f.complete("hello");

2.异步操作

创建异步操作的方法主要是:


public static CompletableFuture<Void> runAsync(Runnable runnable)public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor)public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor)


使用如下:


CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}return "hello";});


String result = f.get();


System.out.println(result);

3.连续异步操作

public CompletableFuture<Void> thenRun(Runnable action)public CompletableFuture<Void> thenRunAsync(Runnable action)public CompletableFuture<Void> thenRunAsync(Runnable action,Executor executor)public <U> CompletableFuture<U> thenApply


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


(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 CompletableFuture<Void> thenAccept(Consumer<? super T> action)public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor)


使用如下:


CompletableFuture<Void> f = CompletableFuture.supplyAsync(() -> "hello").thenApplyAsync(res -> res + " world!").thenAcceptAsync(System.out::println);// wait for job donef.get();

4.等待操作完成

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)


使用如下:


CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> "hello").thenApplyAsync(res -> res + " world!").whenComplete((res, err) -> {if (err != null) {err.printStackTrace();} else {System.out.println(res);}});


// wait for job donef.get();

5.组合

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)public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn, Executor executor)


使用如下:


CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> "Hello").thenCompose(res -> CompletableFuture.supplyAsync(() -> res + " World,")).thenCombine(CompletableFuture.supplyAsync(() -> "CompletableFuture!"), (a, b) -> a + b);


String result = f.get();


System.out.println(result);//Hello World,CompletableFuture!

6.结果 &异常处理

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


使用如下:


// 异常处理 CompletableFuture<Object> f = CompletableFuture.supplyAsync(() -> "Hello").thenApplyAsync(res -> res + "World").thenApplyAsync(res -> {throw new RuntimeException("error");}).exceptionally(e -> {//handle exception heree.printStackTrace();return null;});f.get();


// 执行结果处理 CompletableFuture<Object> f2 = CompletableFuture.supplyAsync(() -> "Hello").thenApplyAsync(res -> res + "World").thenApplyAsync(res -> {throw new RuntimeException("error");}).handleAsync((res, err) -> {if (err != null) {//handle exception herereturn null;} else {return res;}});


Object result = f2.get();


System.out.println(result);

7.并行执行异步操作并统一处理结果

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)使用如下:


CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> "hello");CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> "world");CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> "!");


// 使用 allOf 方法 CompletableFuture<Void> all = CompletableFuture.allOf(f1, f2, f3);all.get();

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
Java8异步编程-CompletableFuture,孔浩java视频百度云盘