66、商城业务---异步---CompletableFuture---handle最终处理

public static ExecutorService service = Executors.newFixedThreadPool(5);
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    System.out.println("当前线程::" + Thread.currentThread().getId());
    int i = 10/0;
    System.out.println("运行结果::" + i);

    return i;
}, service).handle((result, throwable)->{
    if (result!=null){//有返回结果
        return result*2;
    }

    if (throwable!=null){//有异常
        return 0;
    }

    return 0;
});
posted @ 2023-02-07 15:58  不是孩子了  阅读(22)  评论(0)    收藏  举报