多執行緒分段處理List集合
阿新 • • 發佈:2020-07-17
package com.zving.util; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 多執行緒分段處理List集合 * 場景:大資料List集合,需要對List集合中的資料進行處理 * @author clove * */ public class ThreadFun {public static void main(String[] args) { List<String> list = new ArrayList<String>(); for (int i = 1; i <= 3000; i++) { list.add(i + ""); } // 每500條資料開啟一條執行緒 int threadSize = 500; // 總資料條數 int dataSize = list.size(); // 執行緒數int threadNum = dataSize / threadSize + 1; // 定義標記,過濾threadNum為整數 boolean special = dataSize % threadSize == 0; // 建立一個執行緒池 ExecutorService exec = Executors.newFixedThreadPool(threadNum); // 定義一個任務集合 List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(); Callable<Boolean> 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<Boolean>() { @Override public Boolean call() throws Exception { if (null != listStr && listStr.size() > 0) { for (String str : listStr) { // 呼叫業務方法 downloadFile(str); } } return true; } }; tasks.add(task); } try { exec.invokeAll(tasks); } catch (InterruptedException e) { e.printStackTrace(); } // 關閉執行緒池 exec.shutdown(); } private static void downloadFile(String str) { //執行多執行緒檔案下載業務邏輯 System.out.println(str); } }