JUC系列十五:异步回调
作者:互联网
1.1、原理
1.2、实例
package com.atguigu.juc;
import java.util.concurrent.CompletableFuture;
/**
* 异步回调
*/
public class CompletableFutureDemo {
public static void main(String[] args) throws Exception {
//同步
CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName() + "\t completableFuture1");
});
completableFuture1.get();
//异步回调
CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "\t completableFuture2");
int i = 10 / 0;
return 1024;
});
completableFuture2.whenComplete((t, u) -> {
System.out.println("************t = " + t);
System.out.println("************u = " + u);
}).exceptionally(f -> {
System.out.println("--------exception:" + f.getMessage());
return 444;
});
}
}
标签:JUC,异步,completableFuture1,System,completableFuture2,CompletableFuture,十五,println 来源: https://blog.csdn.net/HelloWorld20161112/article/details/122216847