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

前言以前需要异步执行一个任务时,一般是用Thread或者线程池Executor去创建 。如果需要返回值,则是调用Executor.submit获取Future 。但是多个线程存在依赖组合,我们又能怎么办?可使用同步组件CountDownLatch、CyclicBarrier等;其实有简单的方法,就是用CompeletableFuture

  • 线程任务的创建
  • 线程任务的串行执行
  • 线程任务的并行执行
  • 处理任务结果和异常
  • 多任务的简单组合
  • 取消执行线程任务
  • 任务结果的获取和完成与否判断
1 创建异步线程任务根据supplier创建CompletableFuture任务//使用内置线程ForkJoinPool.commonPool(),根据supplier构建执行任务public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)//指定自定义线程,根据supplier构建执行任务public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)1234根据runnable创建CompletableFuture任务//使用内置线程ForkJoinPool.commonPool(),根据runnable构建执行任务public static CompletableFuture<Void> runAsync(Runnable runnable)//指定自定义线程,根据runnable构建执行任务public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)1234
  • 使用示例
ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<Void> rFuture = CompletableFuture.runAsync(() -> System.out.println("hello siting"), executor);//supplyAsync的使用CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {System.out.print("hello ");return "siting";}, executor);//阻塞等待,runAsync 的future 无返回值,输出nullSystem.out.println(rFuture.join());//阻塞等待String name = future.join();System.out.println(name);executor.shutdown(); // 线程池需要关闭--------输出结果--------hello sitingnullhello siting1234567891011121314151617181920常量值作为CompletableFuture返回//有时候是需要构建一个常量的CompletableFuturepublic static <U> CompletableFuture<U> completedFuture(U value)122 线程串行执行
异步编程不会?我教你啊!CompletableFuture

文章插图
【异步编程不会?我教你啊!CompletableFuture】 
任务完成则运行action,不关心上一个任务的结果,无返回值public CompletableFuture<Void> thenRun(Runnable action)public CompletableFuture<Void> thenRunAsync(Runnable action)public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)123
  • 使用示例
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> "hello siting", executor).thenRunAsync(() -> System.out.println("OK"), executor);executor.shutdown();--------输出结果--------OK123456任务完成则运行action,依赖上一个任务的结果,无返回值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)123
  • 使用示例
ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> "hello siting", executor).thenAcceptAsync(System.out::println, executor);executor.shutdown();--------输出结果--------hello siting1234567任务完成则运行fn,依赖上一个任务的结果,有返回值public <U> CompletableFuture<U> thenApply(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)123
  • 使用示例
ExecutorService executor = Executors.newSingleThreadExecutor();CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello world", executor).thenApplyAsync(data -> {System.out.println(data); return "OK";}, executor);System.out.println(future.join());executor.shutdown();--------输出结果--------hello worldOK1234567891011thenCompose - 任务完成则运行fn,依赖上一个任务的结果,有返回值
  • 类似thenApply(区别是thenCompose的返回值是CompletionStage,thenApply则是返回 U),提供该方法为了和其他CompletableFuture任务更好地配套组合使用
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)1234


推荐阅读