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

  • 使用示例
//第一个异步任务,常量任务CompletableFuture<String> f = CompletableFuture.completedFuture("OK");//第二个异步任务ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello world", executor).thenComposeAsync(data -> {System.out.println(data); return f; //使用第一个任务作为返回}, executor);System.out.println(future.join());executor.shutdown();--------输出结果--------hello worldOK12345678910111213143 线程并行执行,合并两任务
异步编程不会?我教你啊!CompletableFuture

文章插图
 
两个CompletableFuture并行执行完,然后执行action,不依赖上两个任务的结果,无返回值public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)123
  • 使用示例
//第一个异步任务,常量任务CompletableFuture<String> first = CompletableFuture.completedFuture("hello world");ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<Void> future = CompletableFuture//第二个异步任务.supplyAsync(() -> "hello siting", executor)// () -> System.out.println("OK") 是第三个任务.runAfterBothAsync(first, () -> System.out.println("OK"), executor);executor.shutdown();--------输出结果--------OK1234567891011两个CompletableFuture并行执行完,然后执行action,依赖上两个任务的结果,无返回值//第一个任务完成再运行other,fn再依赖消费两个任务的结果,无返回值public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action)//两个任务异步完成,fn再依赖消费两个任务的结果,无返回值public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action)//两个任务异步完成(第二个任务用指定线程池执行),fn再依赖消费两个任务的结果,无返回值public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action, Executor executor) 123456789
  • 使用示例
//第一个异步任务,常量任务CompletableFuture<String> first = CompletableFuture.completedFuture("hello world");ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<Void> future = CompletableFuture//第二个异步任务.supplyAsync(() -> "hello siting", executor)// (w, s) -> System.out.println(s) 是第三个任务.thenAcceptBothAsync(first, (s, w) -> System.out.println(s), executor);executor.shutdown();--------输出结果--------hello siting1234567891011两个CompletableFuture并行执行完,然后执行action,依赖上两个任务的结果,有返回值//第一个任务完成再运行other,fn再依赖消费两个任务的结果,有返回值public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)//两个任务异步完成,fn再依赖消费两个任务的结果,有返回值public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn)//两个任务异步完成(第二个任务用指定线程池执行),fn再依赖消费两个任务的结果,有返回值public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn, Executor executor)123456789
  • 使用示例
//第一个异步任务,常量任务CompletableFuture<String> first = CompletableFuture.completedFuture("hello world");ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<String> future = CompletableFuture//第二个异步任务.supplyAsync(() -> "hello siting", executor)// (w, s) -> System.out.println(s) 是第三个任务.thenCombineAsync(first, (s, w) -> {System.out.println(s);return "OK";}, executor);System.out.println(future.join());executor.shutdown();--------输出结果--------hello sitingOK123456789101112131415164 线程并行执行,谁先执行完则谁触发下一任务(二者选其最快)
异步编程不会?我教你啊!CompletableFuture

文章插图
 
上一个任务或者other任务完成, 运行action,不依赖上个任务的结果,无返回值


推荐阅读