public static void main(String[] args) throws ExecutionException, InterruptedException {
// 异步调用,没有返回值
CompletableFuture completableFuture1 = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName()+"completableFuture1");
});
//异步调用有返回值
CompletableFuture completableFuture2=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread().getName()+"completableFuture2");
return 12306;
});
System.out.println(completableFuture2.get());
CompletableFuture completableFuture3=CompletableFuture.supplyAsync(()->{
System.out.println(Thread.currentThread().getName()+"completableFuture3");
int a=1/0;
return 12306;
});
Integer integer = completableFuture3.whenComplete((num, error) -> {
System.out.println("num:" + num);
System.out.println("error:" + error);
}).get();
System.out.println(integer);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)