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(); // 全執(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("結果是: " + 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數據的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:
相關文章
springboot項目中實現訪問druid內置監(jiān)控頁面
這篇文章主要介紹了springboot項目中實現訪問druid內置監(jiān)控頁面的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文)
這篇文章主要介紹了IDEA 創(chuàng)建一個Mybatis Maven項目的方法步驟(圖文),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
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沖突問題,通過注釋掉2.0引入的倆包,直接引入3.0,文中結合實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-10-10

