)import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
class TestCompletableFuture {
public static void main(String[] args) throws Exception {
CompletableFuture future = CompletableFuture.runAsync(()->{//调用没有返回值的方法
System.out.println(Thread.currentThread().getName()+"启动");
});
Object o = future.get();
System.out.println("test"+o); //结果为空
CompletableFuture future1 = CompletableFuture.supplyAsync(()->{//调用有返回值的方法
System.out.println(Thread.currentThread().getName()+"启动");
// int x = 1/0;
return 100;
});
future1.whenComplete((u,t)->{//u 是返回值,t是异常的getMessage()
System.out.println(u);
System.out.println(t);
}).get();//调用get()方法可以获取到返回值,或抛出异常
}
}