CompletableFuture并行处理List分批数据demo

 更新时间:2023年11月03日 09:35:37   作者:丰木  
这篇文章主要介绍了CompletableFuture并行处理List分批数据实现实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

 CompletableFuture并行处理List分批数据

import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.springframework.util.StopWatch;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
 * @author nieweijun
 * @since 2022/2/24 10:53
 */
public class TestCf {
    private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 100, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(512));
    @Test
    public void testTotalAmount() {
        final BigDecimal[] sum = {new BigDecimal(0)};
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        // 1. 假设 sku库存对象的id是0到10000, 先初始化数据id
        List<SkuInventory> skuInventories = IntStream.rangeClosed(1, 10000).boxed()
                .map(i -> SkuInventory.builder().id(String.valueOf(i)).build())
                .collect(Collectors.toList());
        // 2. 查询所有库存, 计算价值总额
        // 模拟每一条数据:批量/每次100个
        List<List<SkuInventory>> partitionsList = Lists.partition(skuInventories, 100);
        // 3. 查算:使用异步
        List<CompletableFuture> cfList = new ArrayList<>();
        CompletableFuture[] cfArray = new CompletableFuture[partitionsList.size()];
        partitionsList.stream().forEach(partition -> {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> queryBatch(partition), executor);
            cfList.add(future);
        });
        CompletableFuture.allOf(cfList.toArray(cfArray)).join(); // 全执行完
        // 4.统计
        skuInventories.stream().forEach(e -> {
            BigDecimal multiply = BigDecimal.valueOf(e.getCount()).multiply(e.getPrice());
            sum[0] = sum[0].add(multiply);
        });
        stopWatch.stop();
        System.out.println("结果是: " + sum[0]);
        System.out.println("耗时毫秒数为: " + stopWatch.getTotalTimeMillis());
    }
    // 模拟查询数据; 设置数据值
    private void queryBatch(List<SkuInventory> partList) {
        if (CollectionUtils.isEmpty(partList)) {
            return;
        }
        long millisCost = RandomUtil.randomInt(100, 1000);
        // 模拟查询耗时
        try {
            TimeUnit.MILLISECONDS.sleep(millisCost);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 模拟赋值: 每个数量为10, 价格1.0
        partList.stream().forEach(e -> {
            e.setCount(10);
            e.setPrice(new BigDecimal("1.0"));
        });
        System.out.println("==========一批partList处理完成耗时[" + millisCost + "ms]==========");
    }
    // sku库存对象
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    static class SkuInventory {
        /** id */
        private String id;
        /** 库存数量 */
        private Integer count;
        /** 单价 */
        private BigDecimal price;
    }
}

以上就是CompletableFuture并行处理List分批数据的详细demo,更多关于CompletableFuture处理List数据的资料请关注脚本之家其它相关文章!

相关文章

  • Java日期时间使用方法汇总

    Java日期时间使用方法汇总

    这篇文章主要针对Java日期时间使用方法进行汇总,感兴趣的朋友可以参考一下
    2016-03-03
  • Java实现简单图书借阅系统

    Java实现简单图书借阅系统

    这篇文章主要为大家详细介绍了Java实现简单图书借阅系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • springboot项目中实现访问druid内置监控页面

    springboot项目中实现访问druid内置监控页面

    这篇文章主要介绍了springboot项目中实现访问druid内置监控页面的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • java中的编码转换过程(以utf8和gbk为例)

    java中的编码转换过程(以utf8和gbk为例)

    这篇文章主要介绍了java中的编码转换过程(以utf8和gbk为例),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • java安全编码指南之:对象构建操作

    java安全编码指南之:对象构建操作

    这篇文章主要介绍了java安全编码指南之:对象构建操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • IDEA 创建一个Mybatis Maven项目的方法步骤(图文)

    IDEA 创建一个Mybatis Maven项目的方法步骤(图文)

    这篇文章主要介绍了IDEA 创建一个Mybatis Maven项目的方法步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Spring MVC内置过滤器功能示例详解

    Spring MVC内置过滤器功能示例详解

    这篇文章主要为大家介绍了Spring MVC内置过滤器使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • java中List<对象>如何根据对象的一个属性进行去重

    java中List<对象>如何根据对象的一个属性进行去重

    这篇文章主要给大家介绍了关于java中List<对象>如何根据对象的一个属性进行去重的相关资料,在开发中可能会遇到很多需要去重的情况,比如Person对象有name跟age两个属性,需要根据age进行去重,需要的朋友可以参考下
    2023-08-08
  • SpringBoot程序打包失败(.jar中没有主清单属性)

    SpringBoot程序打包失败(.jar中没有主清单属性)

    在学习SpringBoot,打包SpringBoot程序后,在cmd运行出现了 某某某.jar中没有注清单属性,本文就来介绍一下原因以及解决方法,感兴趣的可以了解一下
    2023-06-06
  • Swagger3.0 整合spring boot2.7x避免swagger2.0与boot2.7冲突问题

    Swagger3.0 整合spring boot2.7x避免swagger2.0与boot2.7冲突

    这篇文章主要介绍了Swagger3.0 整合spring boot2.7x避免swagger2.0与boot2.7冲突问题,通过注释掉2.0引入的俩包,直接引入3.0,文中结合实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2023-10-10

最新评论