java 8 CompletableFuture (2)

package com.example.demo.future;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class FutureTest {


    /**
     * 获取门店id
     *
     * @return
     */
    private static Long getStoreId() {
        try {

            Thread.sleep(1500); //拟处理远程处理时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Math.round(Math.random() * 10000);
    }

    /**
     * 根据门店Id获取订单数据
     *
     * @return
     */
    private static String getData(String sign) {
        try {

            Thread.sleep(1500); //拟处理远程处理时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return String.format("{storeId:%s,orderNo:'%s',sign:%s}",
                Math.round(Math.random() * 10000), UUID.randomUUID().toString(), sign);
    }


    public static void main(String[] args) {

        CompletableFuture<String> orderFuture = CompletableFuture
                .supplyAsync(FutureTest::getStoreId)   //第一步,从远程先获取门店id
                .thenApply(Long::toHexString)    //第二步,将门店id进行签名
                .thenCompose(sign -> CompletableFuture.supplyAsync(() -> getData(sign)));  //第三步,从远程获取订单数据


    }
}

 

转载于:https://my.oschina.net/u/2552286/blog/1864244

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值