对于的使用可以参照这篇文章:使用Future实现异步回调的方式_大军001的博客-优快云博客
下面主要用代码的方式描述下通过CompletableFuture实现异步获取结果并且等待所有异步任务完成:
/**
* CompletableFuture的AllOf功能测试,等待所有任务执行完
*
*/
public class CompletableFutureAllOfTest {
public static void main(String[] args) throws Exception {
ExecutorService executor = ExecutorServiceSupport.newTheadPool("completableFuture_allOf_test");
method1(executor);
method2(executor);
method3(executor);
}
/**
* 拆解写法
* @param executor
*/
public static void method1 (ExecutorService executor) {
long start = System.currentTimeMillis();
// 定义第一个任务
CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000