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

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

 更新時間:2023年11月03日 09:35:37   作者:豐木  
這篇文章主要介紹了CompletableFuture并行處理List分批數(shù)據(jù)實現(xià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ù)的詳細demo,更多關(guān)于CompletableFuture處理List數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java日期時間使用方法匯總

    Java日期時間使用方法匯總

    這篇文章主要針對Java日期時間使用方法進行匯總,感興趣的朋友可以參考一下
    2016-03-03
  • Java實現(xiàn)簡單圖書借閱系統(tǒng)

    Java實現(xiàn)簡單圖書借閱系統(tǒng)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單圖書借閱系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面

    springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面

    這篇文章主要介紹了springboot項目中實現(xiàn)訪問druid內(nèi)置監(jiān)控頁面的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java中的編碼轉(zhuǎn)換過程(以utf8和gbk為例)

    java中的編碼轉(zhuǎn)換過程(以utf8和gbk為例)

    這篇文章主要介紹了java中的編碼轉(zhuǎn)換過程(以utf8和gbk為例),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • java安全編碼指南之:對象構(gòu)建操作

    java安全編碼指南之:對象構(gòu)建操作

    這篇文章主要介紹了java安全編碼指南之:對象構(gòu)建操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文)

    IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文)

    這篇文章主要介紹了IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Spring?MVC內(nèi)置過濾器功能示例詳解

    Spring?MVC內(nèi)置過濾器功能示例詳解

    這篇文章主要為大家介紹了Spring?MVC內(nèi)置過濾器使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • java中List<對象>如何根據(jù)對象的一個屬性進行去重

    java中List<對象>如何根據(jù)對象的一個屬性進行去重

    這篇文章主要給大家介紹了關(guān)于java中List<對象>如何根據(jù)對象的一個屬性進行去重的相關(guān)資料,在開發(fā)中可能會遇到很多需要去重的情況,比如Person對象有name跟age兩個屬性,需要根據(jù)age進行去重,需要的朋友可以參考下
    2023-08-08
  • SpringBoot程序打包失敗(.jar中沒有主清單屬性)

    SpringBoot程序打包失敗(.jar中沒有主清單屬性)

    在學(xué)習(xí)SpringBoot,打包SpringBoot程序后,在cmd運行出現(xiàn)了 某某某.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,文中結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2023-10-10

最新評論