public class CompletableFutureDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//没有返回值 多线程、功能更强大
// CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
// try {
// TimeUnit.SECONDS.sleep(2);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "没有返回值!");
// });
// System.out.println("111");
// completableFuture.get();
CompletableFuture<Integer> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "supplyAsync");
int i=10/0;
return 1024;
});
uCompletableFuture.whenComplete((t, u) -> {//成功
System.out.println(t+":t");
System.out.println(u+":u");
}).exceptionally(e -> {//失败
System.out.println("e:" +e.getMessage());
return 500;
}).get();
}
Future异步回调
最新推荐文章于 2024-07-12 22:24:08 发布