无返回值
public static void main(String[] args) throws ExecutionException, InterruptedException {
//没有返回值的异步
CompletableFuture completableFuture = new CompletableFuture<>().runAsync(()->{
System.out.println(Thread.currentThread().getName()+"runAsync => Void");
});
System.out.println("ssssss");
completableFuture.get(); //获取阻塞
}
}
有返回值
//有返回值的
CompletableFuture completableFuture = new CompletableFuture<>().supplyAsync(()->{
System.out.println(Thread.currentThread().getName()+"runAsync => Integer");
return 12345;
});
completableFuture.whenComplete((t,u)->{ //成功打印
System.out.println(t);
System.out.println(u);
}).exceptionally((e)->{
System.out.println(e.getMessage()); //失败返回233333
return 233333;
}).get();