public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action, Executor executor)1234
- 使用示例
//第一个异步任务,休眠1秒,保证最晚执行晚CompletableFuture<String> first = CompletableFuture.supplyAsync(()->{try{ Thread.sleep(1000); }catch (Exception e){}System.out.println("hello world");return "hello world";});ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<Void> future = CompletableFuture//第二个异步任务.supplyAsync(() ->{System.out.println("hello siting");return "hello siting";} , executor)//() ->System.out.println("OK") 是第三个任务.runAfterEitherAsync(first, () ->System.out.println("OK") , executor);executor.shutdown();--------输出结果--------hello sitingOK12345678910111213141516171819
上一个任务或者other任务完成, 运行action,依赖最先完成任务的结果,无返回值public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other,Consumer<? super T> action)public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action, Executor executor)public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action, Executor executor)123456
- 使用示例
//第一个异步任务,休眠1秒,保证最晚执行晚CompletableFuture<String> first = CompletableFuture.supplyAsync(()->{try{ Thread.sleep(1000);}catch (Exception e){}return "hello world";});ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<Void> future = CompletableFuture//第二个异步任务.supplyAsync(() -> "hello siting", executor)// data ->System.out.println(data) 是第三个任务.acceptEitherAsync(first, data ->System.out.println(data) , executor);executor.shutdown();--------输出结果--------hello siting1234567891011121314
上一个任务或者other任务完成, 运行fn,依赖最先完成任务的结果,有返回值public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn) public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn)public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn, Executor executor)123456
- 使用示例
//第一个异步任务,休眠1秒,保证最晚执行晚CompletableFuture<String> first = CompletableFuture.supplyAsync(()->{try{ Thread.sleep(1000);}catch (Exception e){}return "hello world";});ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<String> future = CompletableFuture//第二个异步任务.supplyAsync(() -> "hello siting", executor)// data ->System.out.println(data) 是第三个任务.applyToEitherAsync(first, data ->{System.out.println(data);return "OK";} , executor);System.out.println(future);executor.shutdown();--------输出结果--------hello sitingOK12345678910111213141516171819
5 处理任务结果或者异常exceptionally-处理异常文章插图
public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)1
- 如果之前的处理环节有异常问题,则会触发exceptionally的调用相当于try…catch
- 使用示例
CompletableFuture<Integer> first = CompletableFuture.supplyAsync(() -> {if (true) {throw new RuntimeException("main error!");}return "hello world";}).thenApply(data -> 1).exceptionally(e -> {e.printStackTrace(); // 异常捕捉处理,前面两个处理环节的日常都能捕获return 0;});123456789101112
handle-任务完成或者异常时运行fn,返回值为fn的返回- 相比exceptionally而言,即可处理上一环节的异常也可以处理其正常返回值
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)1234
- 使用示例
CompletableFuture<Integer> first = CompletableFuture.supplyAsync(() -> {if (true) { throw new RuntimeException("main error!"); }return "hello world";}).thenApply(data -> 1).handleAsync((data,e) -> {e.printStackTrace(); // 异常捕捉处理return data;});System.out.println(first.join());--------输出结果--------JAVA.util.concurrent.CompletionException: java.lang.RuntimeException: main error! ... 5 morenull123456789101112131415
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 史书上不会公开的历史真相 中国古代历史未解之谜
- 葡萄会不会上火 吃萄葡会不会上火
- 明年会不会干旱 冬天为什么会干旱
- 今年冬天会不会很冷2021 2021是不是最冷的冬天
- 网编基础,我只看这篇文章,网络编程基础篇
- Win10将原生兼容安卓App,但模拟器不会被打败
- 化妆|“会化妆”与“不会化妆”的女人,看眉毛和口红就知道:差别太大
- 自动加热的泡脚桶会漏电吗 泡脚桶会不会漏电致死
- 口腔纤维化戒掉槟榔会不会恢复? 吃槟榔为什么会导致口腔纤维化
- selsun洗发水会不会脱发 selsun洗发水可以长期使用吗