异步编程不会?我教你啊!CompletableFuture( 三 )

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 sitingOK123456789101112131415161718195 处理任务结果或者异常exceptionally-处理异常
异步编程不会?我教你啊!CompletableFuture

文章插图
 
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;});123456789101112handle-任务完成或者异常时运行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


推荐阅读