CompletableFuture方法全解

public class SpringbootWebApplicationTests {

    private final Logger logger = LoggerFactory.getLogger(SpringbootWebApplicationTests.class);

    private static final ExecutorService threadPool = Executors.newFixedThreadPool(5);

    @Test
    public void testCompletableFuture() throws Exception {

        // 1、第一组
        CompletableFuture.runAsync(() -> {
            logger.info("runAsync--无返回值");
        });
        CompletableFuture.runAsync(() -> {
            logger.info("runAsync--无返回值--指定线程池");
        }, threadPool);

        // 2、第二组
        CompletableFuture<Integer> supplyAsync1 = CompletableFuture.supplyAsync(() -> {
            logger.info("supplyAsync--有返回值");
            return 1;
        });
        CompletableFuture<Integer> supplyAsync2 = CompletableFuture.supplyAsync(() -> {
            logger.info("supplyAsync--有返回值--指定线程池");
            return 1;
        }, threadPool);

        // 3,任务回调
        // 3.1
        // thenApply和他的父任务在一个线程里
        CompletableFuture<String> thenApply1 = supplyAsync1.thenApply((result) -> {
            logger.info("thenApply:supplyAsync1执行完毕后的回调,参数为supplyAsync1任务的返回值");
            return "result:" + result;
        });
        // thenApplyAsync和他的父任务可能不在一个线程里
        CompletableFuture<String> thenApply2 = supplyAsync2.thenApplyAsync((result) -> {
            logger.info("thenApplyAsync:supplyAsync2执行完毕后的回调,参数为supplyAsync2任务的返回值");
            return "result:" + result;
        });
        // 可以指定线程池
        CompletableFuture<String> thenApply3 = supplyAsync2.thenApplyAsync((result) -> {
            logger.info("thenApplyAsync--指定线程池:supplyAsync2执行完毕后的回调,参数为supplyAsync2任务的返回值");
            return "result:" + result;
        }, threadPool);

        // 3.2
        CompletableFuture<Void> thenAccept = thenApply1.thenAccept((result) -> {
            logger.info("thenAccept:thenApply1执行完毕后的回调,参数为thenApply1任务的返回值,没有返回值");
            int i = 1 / 0;
        });
        CompletableFuture<Void> thenRun = thenApply2.thenRun(() -> {
            logger.info("thenRun:thenApply2执行完毕后的回调,没有参数,也没有返回值");
        });

        // 3.3
        thenAccept.exceptionally((exception) -> {
            logger.info("exceptionally:任务异常后的回调,参数为异常信息");
            logger.info("异常信息", exception);
            return null;
        });
        thenApply3.whenComplete((result, exception) -> {
            logger.info("{},{}", result, exception);
            logger.info("whenComplete:任务异常后的回调,会将执行结果或者执行期间抛出的异常传递给回调方法,如果是正常执行则异常为null");
        });
        thenApply3.whenCompleteAsync((result, exception) -> {
            logger.info("{},{}", result, exception);
            logger.info("whenCompleteAsync:任务异常后的回调,会将执行结果或者执行期间抛出的异常传递给回调方法,如果是正常执行则异常为null");
        });
        thenApply3.whenCompleteAsync((result, exception) -> {
            logger.info("{},{}", result, exception);
            logger.info("whenCompleteAsync--指定线程池:任务异常后的回调,会将执行结果或者执行期间抛出的异常传递给回调方法,如果是正常执行则异常为null");
        }, threadPool);

        logger.info("supplyAsync获取返回值:" + supplyAsync1.get() + "--" + supplyAsync2.get());
        logger.info("thenApply获取返回值:" + thenApply1.get() + "--" + thenApply2.get() + "--" + thenApply3.get());
    }

}
posted @ 2022-03-17 16:25  Liming_Code  阅读(198)  评论(0编辑  收藏  举报