CompletableFutrue通过核心的4个静态方法,来创建一个异步任务
- public static CompletableFuture<Void> runAsync(Runnable runnable)
1 /** 2 * public static CompletableFuture<Void> runAsync(Runnable runnable) 不指定线程池, 默认使用 ForkJoinPool.commonPool 3 */ 4 public class CompletableFutrueDemo { 5 public static void main(String[] args) throws ExecutionException, InterruptedException { 6 7 CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> { 8 9 System.out.println(Thread.currentThread().getName()); 10 11 try { 12 TimeUnit.SECONDS.sleep(3); 13 } catch (InterruptedException e) { 14 e.printStackTrace(); 15 } 16 }); 17 18 System.out.println(completableFuture.get()); 19 } 20 }
- public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
- public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
- public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
上述Executor参数说明
没有指定Excutor的方法,直接使用默认的ForkJoinPool.commonPool()作为它的线程池执行异步代码
如果指定线程池,则使用我们自定义的或者特别指定的线程池执行异步代码
标签:JUC,静态方法,runAsync,线程,static,CompletableFutrue,CompletableFuture,public From: https://www.cnblogs.com/zhaohan258/p/18234049