先看一个栗子
可以通过CompletableFuture.runAsyns()创建一个异步task,也就是说通过
CompletableFuture.runAsyns()创建的task在执行的时候并不会阻塞主线程,这样会很大程度上提高代码的效率。
public class DiagCompletable {
public static void main(String[] args) throws InterruptedException {
completable();
}
public static void completable() throws InterruptedException {
/**
* 1.CompletableFuture.runAsync()可以创建一个异步任务
* 2.CompletableFuture.allOf(arrs)是一个同步操作,等到arrs中的所有future都执行完成在执行后面的thenApplyAsync()方法
* 3.CompletableFuture的异步任务不会阻塞主线程(main方法线程)
*/
List<CompletableFuture<Void>> futures = new ArrayList<>();
for(int i=0; i<5; i++){
CompletableFuture<Void> f = CompletableFuture.runAsync(()->{
log.info("create future ...");
try {
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("create future completed");
}).thenApply((x)->{
log.info("process then apply");
return null;
});
futures.add(f);
}
CompletableFuture<Void>[] arrs = new CompletableFuture[futures.size()];
arrs = futures.toArray(arrs);
AtomicBoolean isDone = new AtomicBoolean(false);
CompletableFuture.allOf(arrs).thenApplyAsync(x->{
try {
Thread.sleep(1000 * 5);
isDone.set(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("all future completed");
return null;
});
while (true){
if(isDone.get()){
break;
}
log.info("is all task completed");
Thread.sleep(1000 * 2);
}
}
}
输出结果:
> Task :DiagCompletable.main()
10:03:29.423 [main] INFO com.troyqu.refactor.DiagCompletable - is all task completed
10:03:29.423 [ForkJoinPool.commonPool-worker-3] INFO com.troyqu.refactor.DiagCompletable - create future ...
10:03:29.423 [ForkJoinPool.commonPool-worker-2] INFO com.troyqu.refactor.DiagCompletable - create future ...
10:03:29.423 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - create future ...
10:03:31.428 [main] INFO com.troyqu.refactor.DiagCompletable - is all task completed
10:03:31.429 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - create future completed
10:03:31.429 [ForkJoinPool.commonPool-worker-2] INFO com.troyqu.refactor.DiagCompletable - create future completed
10:03:31.430 [ForkJoinPool.commonPool-worker-2] INFO com.troyqu.refactor.DiagCompletable - process then apply
10:03:31.430 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - process then apply
10:03:31.430 [ForkJoinPool.commonPool-worker-2] INFO com.troyqu.refactor.DiagCompletable - create future ...
10:03:31.430 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - create future ...
10:03:31.429 [ForkJoinPool.commonPool-worker-3] INFO com.troyqu.refactor.DiagCompletable - create future completed
10:03:31.430 [ForkJoinPool.commonPool-worker-3] INFO com.troyqu.refactor.DiagCompletable - process then apply
10:03:33.429 [main] INFO com.troyqu.refactor.DiagCompletable - is all task completed
10:03:33.431 [ForkJoinPool.commonPool-worker-2] INFO com.troyqu.refactor.DiagCompletable - create future completed
10:03:33.431 [ForkJoinPool.commonPool-worker-2] INFO com.troyqu.refactor.DiagCompletable - process then apply
10:03:33.433 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - create future completed
10:03:33.434 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - process then apply
10:03:35.434 [main] INFO com.troyqu.refactor.DiagCompletable - is all task completed
10:03:37.439 [main] INFO com.troyqu.refactor.DiagCompletable - is all task completed
10:03:38.439 [ForkJoinPool.commonPool-worker-1] INFO com.troyqu.refactor.DiagCompletable - all future completed