java 多執行緒實現任務分發
阿新 • • 發佈:2019-02-07
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();
}
}
}
存在問題:怎樣知道哪個執行緒執行哪些任務
總共包含三個類 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();
}
}
}