欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CompletableFuture并行處理List分批數(shù)據(jù)demo

 更新時間:2023年11月03日 09:35:37   作者:豐木  
這篇文章主要介紹了CompletableFuture并行處理List分批數(shù)據(jù)實(shí)現(xiàn)實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

 CompletableFuture并行處理List分批數(shù)據(jù)

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. 假設(shè) sku庫存對象的id是0到10000, 先初始化數(shù)據(jù)id
        List<SkuInventory> skuInventories = IntStream.rangeClosed(1, 10000).boxed()
                .map(i -> SkuInventory.builder().id(String.valueOf(i)).build())
                .collect(Collectors.toList());
        // 2. 查詢所有庫存, 計算價值總額
        // 模擬每一條數(shù)據(jù):批量/每次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(); // 全執(zhí)行完
        // 4.統(tǒng)計
        skuInventories.stream().forEach(e -> {
            BigDecimal multiply = BigDecimal.valueOf(e.getCount()).multiply(e.getPrice());
            sum[0] = sum[0].add(multiply);
        });
        stopWatch.stop();
        System.out.println("結(jié)果是: " + sum[0]);
        System.out.println("耗時毫秒數(shù)為: " + stopWatch.getTotalTimeMillis());
    }
    // 模擬查詢數(shù)據(jù); 設(shè)置數(shù)據(jù)值
    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();
        }
        // 模擬賦值: 每個數(shù)量為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;
        /** 庫存數(shù)量 */
        private Integer count;
        /** 單價 */
        private BigDecimal price;
    }
}

以上就是CompletableFuture并行處理List分批數(shù)據(jù)的詳細(xì)demo,更多關(guān)于CompletableFuture處理List數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論