#CompletableFuture 常用示例CompletableFuture<String> completableFuture = new CompletableFuture<String>(); String result = completableFuture.get() // get() 方法会一直阻塞直到 Future 完成。 CompletableFuture.complete() // 手工的完成一个 Future: //TODO 最好传入自定义线程池, or use ForkJoinPool.commonPool() default //async no need return . static CompletableFuture<Void> runAsync(Runnable runnable) static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor) // async need return static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) // Using Lambda Expression CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } return "Result of the asynchronous computation"; }); thenApply // 需要返回值 thenAccept() 和 thenRun() // 不需要从回调函数中返回任何东西 // thenAccept() example CompletableFuture.supplyAsync(() -> { return ProductService.getProductDetail(productId); }).thenAccept(product -> { System.out.println("Got product detail from remote service " + product.getName()) }); 虽然thenAccept()可以访问CompletableFuture的结果,但thenRun()不能访Future的结果,它持有一个Runnable返回CompletableFuture: thenApplyAsync // 通过在独立的线程中执行回调任务帮助你进一步执行并行计算 thenCompose()被用于当一个future依赖另外一个future的时候用来组合两个future。 thenCombine() 被用来当两个独立的Future都完成的时候 static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) https://juejin.cn/post/6844903594165026829#heading-0