针对任务分发已知:
1:需要被执行的任务列表,2:启动的线程数.
存在问题:怎样知道哪个线程执行哪些任务
总共包含三个类 1:taskdistributor 任务分发器,2:待执行的任务,3:工作线程。
Taskdistributor.java
import java.util.ArrayList;
import java.util.List;
public class TaskDistributor {
public static void main(String[] args) {
List<Task> taskList = new ArrayList<Task>();
for (int i = 0; i < 56; i++) {
taskList.add(new Task(i));
}
int threadCount = 5;
List[] taskPerThreadCount = distributeTasks(taskList, threadCount);
System.out.println("实际要启动的工作线程:" + taskPerThreadCount.length);
for (int i = 0; i < taskPerThreadCount.length; i++) {
Thread workThread = new WorkThread(taskPerThreadCount[i], i);
workThread.start();
}
}
public static List[] distributeTasks(List<Task> taskList, int threadCount) {
// 每个线程至少要执行的任务数,如果不为零,则每个线程都会分到任务;
int minTaskCount = taskList.size() / threadCount;
System.out.println(minTaskCount + " minTaskCount");
// 剩下的任务数,如果不为零,则依个添加到前面的线程中.
int remainTaskCount = taskList.size() % threadCount;
System.out.println(remainTaskCount + " remainTaskCount");
// 实际要启动的线程个数,如果工作线程比较任务还多,则只需要启动与任务数相同的工作
// 线程,一对一工作,
int actualThreadCount = minTaskCount > 0 ? threadCount
: remainTaskCount;
System.out.println(actualThreadCount + " actualThreadCount");
// 要启动的线程数组,以及每个线程执行的任务列表.
List<Task>[] taskListPerThread = new List[actualThreadCount];
int taskIndex = 0;
int remainIndces = remainTaskCount;
for (int i = 0, n = taskListPerThread.length; i < n; 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, n = taskListPerThread.length; i < n; i++) {
System.out.println("线程 "
+ i
+ " 的任务数:"
+ taskListPerThread[i].size()
+ " 区间["
+ taskListPerThread[i].get(0).getTaskId()
+ ","
+ taskListPerThread[i].get(taskListPerThread[i].size() - 1)
.getTaskId() + "]");
}
return taskListPerThread;
}
}
2:task 任务:
public class Task {
private static final int READY = 0;
private static final int RUNNING = 1;
private static final int FINISHED = 2;
private int status;
private int taskId;
public Task(int taskid) {
this.status = READY;
this.taskId = taskid;
}
public void execute() {
try {
setStatus(RUNNING);
System.out.println("当前的线程ID是:" + Thread.currentThread().getName()
+ " 任务ID是:" + taskId);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setStatus(FINISHED);
}
public void setStatus(int status) {
this.status = status;
}
public int getTaskId() {
return taskId;
}
}
3: WorkThread 自定义工作线程
public class WorkThread extends Thread {
private List<Task> taskList = null;
private int threadId;
public WorkThread(List<Task> taskList, int threadid) {
this.taskList = taskList;
this.threadId = threadid;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (Task task : taskList) {
task.execute();
}
}
}
运行结果:
线程 0 的任务数:12 区间[0,11]
线程 1 的任务数:11 区间[12,22]
线程 2 的任务数:11 区间[23,33]
线程 3 的任务数:11 区间[34,44]
线程 4 的任务数:11 区间[45,55]
实际要启动的工作线程:5
当前的线程ID是:Thread-5 任务ID是:0
当前的线程ID是:Thread-6 任务ID是:12
当前的线程ID是:Thread-7 任务ID是:23
当前的线程ID是:Thread-9 任务ID是:45
当前的线程ID是:Thread-8 任务ID是:34
当前的线程ID是:Thread-6 任务ID是:13
当前的线程ID是:Thread-5 任务ID是:1
当前的线程ID是:Thread-8 任务ID是:35
当前的线程ID是:Thread-7 任务ID是:24
当前的线程ID是:Thread-9 任务ID是:46
当前的线程ID是:Thread-5 任务ID是:2
当前的线程ID是:Thread-7 任务ID是:25
当前的线程ID是:Thread-8 任务ID是:36
当前的线程ID是:Thread-6 任务ID是:14
当前的线程ID是:Thread-9 任务ID是:47
当前的线程ID是:Thread-7 任务ID是:26
当前的线程ID是:Thread-5 任务ID是:3
当前的线程ID是:Thread-8 任务ID是:37
当前的线程ID是:Thread-6 任务ID是:15
当前的线程ID是:Thread-9 任务ID是:48
当前的线程ID是:Thread-5 任务ID是:4
当前的线程ID是:Thread-8 任务ID是:38
当前的线程ID是:Thread-7 任务ID是:27
当前的线程ID是:Thread-6 任务ID是:16
当前的线程ID是:Thread-9 任务ID是:49
当前的线程ID是:Thread-7 任务ID是:28
当前的线程ID是:Thread-5 任务ID是:5
以上只是列出了线程执行的部分任务。