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); } }