200、商城业务-异步-CompletableFuture-两任务组合-都要完成

         /**
         * 两个都完成
         */
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务1结束:");
            return i;
        }, executor);

        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            System.out.println("任务2结束:");
            return "Hello";
        }, executor);

        future01.runAfterBothAsync(future02,()->{
            System.out.println("任务3开始");
        }, executor);
        future01.thenAcceptBothAsync(future02,(f1, f2)->{
            System.out.println("任务3开始。。。之前的结果:" + f1 + "==>" + f2);
        }, executor);
        System.out.println("main....end....." );

/**
         * 两个都完成
         */
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务1结束:");
            return i;
        }, executor);

        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            System.out.println("任务2结束:");
            return "Hello";
        }, executor);

//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务3开始");
//        }, executor);
//        future01.thenAcceptBothAsync(future02,(f1, f2)->{
//            System.out.println("任务3开始。。。之前的结果:" + f1 + "==>" + f2);
//        }, executor);
//        System.out.println("main....end....." );

        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
            return f1 + ":" + f2 + " -> Haha";
        }, executor);

        System.out.println("main....end....." + future.get());

猜你喜欢

转载自blog.csdn.net/pyd1040201698/article/details/108445666