CompletableFuture并行處理List分批數(shù)據(jù)demo
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庫(kù)存對(duì)象的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. 查詢所有庫(kù)存, 計(jì)算價(jià)值總額
// 模擬每一條數(shù)據(jù):批量/每次100個(gè)
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)計(jì)
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í)毫秒數(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);
// 模擬查詢耗時(shí)
try {
TimeUnit.MILLISECONDS.sleep(millisCost);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 模擬賦值: 每個(gè)數(shù)量為10, 價(jià)格1.0
partList.stream().forEach(e -> {
e.setCount(10);
e.setPrice(new BigDecimal("1.0"));
});
System.out.println("==========一批partList處理完成耗時(shí)[" + millisCost + "ms]==========");
}
// sku庫(kù)存對(duì)象
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
static class SkuInventory {
/** id */
private String id;
/** 庫(kù)存數(shù)量 */
private Integer count;
/** 單價(jià) */
private BigDecimal price;
}
}以上就是CompletableFuture并行處理List分批數(shù)據(jù)的詳細(xì)demo,更多關(guān)于CompletableFuture處理List數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
springboot項(xiàng)目中實(shí)現(xiàn)訪問(wèn)druid內(nèi)置監(jiān)控頁(yè)面
這篇文章主要介紹了springboot項(xiàng)目中實(shí)現(xiàn)訪問(wèn)druid內(nèi)置監(jiān)控頁(yè)面的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java中的編碼轉(zhuǎn)換過(guò)程(以u(píng)tf8和gbk為例)
這篇文章主要介紹了java中的編碼轉(zhuǎn)換過(guò)程(以u(píng)tf8和gbk為例),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
IDEA 創(chuàng)建一個(gè)Mybatis Maven項(xiàng)目的方法步驟(圖文)
這篇文章主要介紹了IDEA 創(chuàng)建一個(gè)Mybatis Maven項(xiàng)目的方法步驟(圖文),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Spring?MVC內(nèi)置過(guò)濾器功能示例詳解
這篇文章主要為大家介紹了Spring?MVC內(nèi)置過(guò)濾器使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
java中List<對(duì)象>如何根據(jù)對(duì)象的一個(gè)屬性進(jìn)行去重
這篇文章主要給大家介紹了關(guān)于java中List<對(duì)象>如何根據(jù)對(duì)象的一個(gè)屬性進(jìn)行去重的相關(guān)資料,在開(kāi)發(fā)中可能會(huì)遇到很多需要去重的情況,比如Person對(duì)象有name跟age兩個(gè)屬性,需要根據(jù)age進(jìn)行去重,需要的朋友可以參考下2023-08-08
SpringBoot程序打包失敗(.jar中沒(méi)有主清單屬性)
在學(xué)習(xí)SpringBoot,打包SpringBoot程序后,在cmd運(yùn)行出現(xiàn)了 某某某.jar中沒(méi)有注清單屬性,本文就來(lái)介紹一下原因以及解決方法,感興趣的可以了解一下2023-06-06
Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突
這篇文章主要介紹了Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突問(wèn)題,通過(guò)注釋掉2.0引入的倆包,直接引入3.0,文中結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10

