import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTest {
public static ExecutorService service = Executors.newFixedThreadPool(5);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main-----start");
// CompletableFuture.runAsync(()->{
// System.out.println("当前线程::" + Thread.currentThread().getId());
// int i = 10/2;
// System.out.println("运行结果::" + i);
// }, service);
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程::" + Thread.currentThread().getId());
int i = 10/0;
System.out.println("运行结果::" + i);
return i;
}, service).whenComplete((result, exception)->{
/**
* 虽然能得到异常信息,但是没有返回值,也就没法修改返回数据
*/
System.out.println("结果是::" + result);
System.out.println("异常是::" + exception);
}).exceptionally(throwable -> {
/**
* 可以修改返回数据
*/
System.out.println("异常是::" + throwable);
return 10;
});
System.out.println("main-----end--" + future.get());
}
}
标签:java,System,---,CompletableFuture,65,println,out
From: https://www.cnblogs.com/morehair/p/17098762.html