CompletableFuture.thenCompose

文章目录

thenCompose 学习

thenCompose允许将两个异步操作进行流水线,第一个操作完成时,将其结果作为参数传递给第二个操作。

package book.java8.char11;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/20 10:27
 */
public class CompletableFutureDemo {
    /**
     * json样式的 字符串
     */
    static class ToStrJson {
        public static String toJson(IsDigits isDigits) {
            delay();
            final String result = ToStringBuilder.reflectionToString(isDigits, ToStringStyle.JSON_STYLE);
            System.out.println("ToStrJson:" + result);
            return result;
        }
    }

    static class IsDigits {
        /**
         * 字符串
         */
        private final String str;
        /**
         * str中包含的数字
         */
        private final String digit;

        public IsDigits(String str, String digit) {
            this.str = str;
            this.digit = digit;
        }

        public static IsDigits parse(String str) {
            //字符串中提取数据
            return new IsDigits(str, StringUtils.getDigits(str));
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
        }
    }

    private static void delay() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static String apply() {
        delay();
        return RandomStringUtils.randomAlphanumeric(5);
    }

    /**
     * testMap 和testCompletableFuture 效果一样,为了测试 map和 CompletableFuture (thenCompose)
     * <p>
     * thenCompose允许将两个异步操作进行流水线,第一个操作完成时,将其结果作为参数传递给第二个操作。
     * 换句话说,可以创建两个两个CompletableFutre对象,对第一个CompletableFuture调用
     * thenCompose,并向其传递一个函数,
     * </p>
     */
    public static void testMap() {
        final List<String> collect = IntStream.rangeClosed(1, 10).mapToObj(i -> {
            final String apply = apply();
            System.out.println("生成的随机字符串:" + apply);
            return apply;
        }).map(IsDigits::parse)//字符串转换成 IsDigits
                .map(ToStrJson::toJson)// IsDigits 转换 json字符串
                .collect(Collectors.toList());
    }

    public static void testCompletableFuture() {
        final List<CompletableFuture<String>> collect = IntStream.rangeClosed(1, 10).mapToObj(i ->
                CompletableFuture.supplyAsync(() -> {
                            final String apply = apply();
                            System.out.println("生成的随机字符串:" + apply);
                            return apply;
                        }
                )
        ).map(future -> future.thenApply(IsDigits::parse))
                .map(future -> future.thenCompose(isDigits ->
                        CompletableFuture.supplyAsync(
                                () -> ToStrJson.toJson(isDigits))))
                .collect(Collectors.toList());
        //等待 执行完毕
        collect.stream().map(CompletableFuture::join).collect(toList());
    }

    public static void main(String[] args) throws InterruptedException {
        final long start = System.nanoTime();
        testCompletableFuture();
        System.out.println();
        System.out.println("执行时间:" + (System.nanoTime() - start) / 1_000_000);
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值