import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class FutureTaskDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
/* 无返回值异步回调
CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(()->{
System.out.println("test1");
});
System.out.println("test2");
completableFuture.get();
*/
/* 有返回值异步回调
CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
return 200;
});
System.out.println(completableFuture.whenComplete((t,u)->{
System.out.println(t); //正常返回结果
System.out.println(u); //错误信息 java.util.concurrent.CompletionException
}).exceptionally((e)->{
e.getMessage();
return 500;
}).get());
*/
}
}