public static List[] distributeTasks(List taskList, int threadCount) { int minTaskCount = taskList.size() / threadCount; int remainTaskCount = taskList.size() % threadCount; int actualThreadCount = minTaskCount > 0 ? threadCount : remainTaskCount; List[] taskListPerThread = new List[actualThreadCount]; int taskIndex = 0; int remainIndces = remainTaskCount; for (int i = 0; i < taskListPerThread.length; i++) { taskListPerThread[i] = new ArrayList(); if (minTaskCount > 0) { for (int j = taskIndex; j < minTaskCount + taskIndex; j++) { taskListPerThread[i].add(taskList.get(j)); } taskIndex += minTaskCount; } if (remainIndces > 0) { taskListPerThread[i].add(taskList.get(taskIndex++)); remainIndces--; } } for (int i = 0; i < taskListPerThread.length; i++) { System.out.println("线程 " + i + " 的任务数:" + taskListPerThread[i].size() + " 区间[" + taskListPerThread[i].get(0).getTaskId() + "," + taskListPerThread[i].get(taskListPerThread[i].size() - 1).getTaskId() + "]"); } return taskListPerThread; } } class Task { public static |