Java多線程處理List問題
更新時間:2023年09月21日 16:25:44 作者:JonTang
這篇文章主要介紹了Java多線程處理List問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Java多線程處理List
項目場景
調用第三方提供的接口去獲取 List 中用戶的組信息。
問題描述
需要拿用戶的 id 去調用第三方接口,成功調用一次需要 0.3s 左右,當有 1000 個用戶時,就需要花費 0.3 * 1000s = 5min,頁面就會一直加載那么久。
之前是通過 for 循環(huán) list 去調用接口的,
代碼如下:
// 當 list 長度為 1000時,則需要循環(huán) 1000次 for(User user : list) { loadUserGroups(user); }
解決方案
通過多線程的方式去處理,話不多說直接上代碼:
// 定義一個線程池 private static final ExecutorService loadUserGroupsExecutor = Executors.newFixedThreadPool(20); public Map<String, List<UserGroup>> loadAllUserGroups() { Map<String, List<UserGroup>> userGroups = new ConcurrentHashMap<>(); List<User> users = listUsers(); int size = users.size(); long startTime = System.currentTimeMillis(); if (size > 200) { List<List<User>> partition = Lists.partition(users, 200); List<CompletableFuture> results = new ArrayList<>(); for (List<User> subList : partition) { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { loadUserGroups(userGroups, subList); return ""; }, loadUserGroupsExecutor); results.add(future); } CompletableFuture.allOf(results.toArray(results.toArray(new CompletableFuture[partition.size()]))).join(); } else { loadUserGroups(userGroups, users); } log.info("loadAllUserGroups cost {}", System.currentTimeMillis() - startTime); return userGroups; }
Java多線程分段處理List集合
場景:
大數據List集合,需要對List集合中的數據同標準庫中數據進行對比,生成新增,更新,取消數據
解決方案
- List集合分段
- 動態(tài)創(chuàng)建線程池newFixedThreadPool
- 將對比操作在多線程中實現
public static void main(String[] args) throws Exception { ?? ?// 開始時間 ?? ?long start = System.currentTimeMillis(); ?? ?List<String> list = new ArrayList<String>(); ?? ?for (int i = 1; i <= 3000; i++) { ?? ??? ?list.add(i + ""); ?? ?} ? ? /*動態(tài)線程數方式*/ ?? ?// 每500條數據開啟一條線程 ?? ?int threadSize = 500; ?? ?// 總數據條數 ?? ?int dataSize = list.size(); ?? ?// 線程數,動態(tài)生成 ?? ?int threadNum = dataSize / threadSize + 1; ? ? /*固定線程數方式 ?? ? ? ?// 線程數 ?? ? ? ?int threadNum = 6; ?? ? ? ?// 總數據條數 ?? ? ? ?int dataSize = list.size(); ?? ? ? ?// 每一條線程處理多少條數據 ?? ? ? ?int threadSize = dataSize / (threadNum - 1); ? ? */ ?? ?// 定義標記,過濾threadNum為整數 ?? ?boolean special = dataSize % threadSize == 0; ?? ?// 創(chuàng)建一個線程池 ?? ?ExecutorService exec = Executors.newFixedThreadPool(threadNum); ?? ?// 定義一個任務集合 ?? ?List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); ?? ?Callable<Integer> task = null; ?? ?List<String> cutList = null; ?? ?// 確定每條線程的數據 ?? ?for (int i = 0; i < threadNum; i++) { ?? ??? ?if (i == threadNum - 1) { ?? ??? ??? ?if (special) { ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?cutList = list.subList(threadSize * i, dataSize); ?? ??? ?} else { ?? ??? ??? ?cutList = list.subList(threadSize * i, threadSize * (i + 1)); ?? ??? ?} ?? ??? ?final List<String> listStr = cutList; ?? ??? ?task = new Callable<Integer>() { ?? ??? ??? ?@Override ?? ??? ??? ?public Integer call() throws Exception { ?? ??? ??? ??? ?//業(yè)務邏輯,循環(huán)處理分段后的list ?? ??? ??? ??? ?System.out.println(Thread.currentThread().getName() + "線程:" + listStr); ?? ??? ??? ??? ?//...... ?? ??? ??? ??? ?return 1; ?? ??? ??? ?} ?? ??? ?}; ?? ??? ?// 這里提交的任務容器列表和返回的Future列表存在順序對應的關系 ?? ??? ?tasks.add(task); ?? ?} ?? ?exec.invokeAll(tasks); ?? ?// 關閉線程池 ?? ?exec.shutdown(); ?? ?System.out.println("線程任務執(zhí)行結束"); ?? ?System.out.println("執(zhí)行任務消耗了 :" + (System.currentTimeMillis() - start) + "毫秒"); }
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Cloud Alibaba使用Sentinel實現接口限流
這篇文章主要介紹了Spring Cloud Alibaba使用Sentinel實現接口限流,本文詳細的介紹了Sentinel組件的用法以及接口限流,感興趣的可以了解一下2019-04-04SpringBoot利用Validation包實現高效參數校驗
如果不進行校驗就直接使用這些數據,可能會導致各種問題,那么SpringBoot如何利用Validation包實現高效參數校驗呢,下面讓我們一起來探討這個重要的話題吧2025-04-04springboot全局異常處理方式@ControllerAdvice和@ExceptionHandler
文章總結了個人在處理全局異常處理時的經驗,包括使用`StatusEnum`來定義狀態(tài)碼,旨在為讀者提供參考,并鼓勵大家支持腳本之家2024-11-11