创建异步任务
CompletableFuture
创建异步任务,一般有 supplyAsync
和 runAsync
两个方法
supplyAsync执行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)
runAsync执行CompletableFuture任务,有返回值
//使用默认内置线程池ForkJoinPool.commonPool(),根据runnable构建执行任务
public static CompletableFuture<Void> runAsync(Runnable runnable)
//自定义线程,根据runnable构建执行任务
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
任务异步回调
文章转载自异步编程利器:CompletableFuture详解 |Java 开发实战