我有一个需要按特定顺序执行的任务列表,例如 Task2 -> Task1,因此必须先执行 Task1,然后才能运行任务 Task2。我已经构建了一个任务图并应用了拓扑排序来获取任务执行的顺序,但是现在我想使用线程 abd 执行这些任务,这就是我卡住的地方。这是我到目前为止所做的
//S = Hashset that contains tasks that has no input edges in graph
public HashSet<Task> S = new HashSet<Task> ();
for(Task n : AllTasks){ //AllTasks is an arraylist of tasks in graph
if(n.inEdges.size() == 0){
S.add(n);}
}
//将包含已排序元素的空列表 ArrayList outList = new ArrayList();
//while S is non-empty do
while(!G.S.isEmpty()){
//remove a node n from S
Task n = G.S.iterator().next();
G.S.remove(n);
//insert n into L
outList.add(n);
n.start(); //Starts the thread
//for each node m with an edge e from n to m do
for(Iterator<Edge> it = n.outEdges.iterator();it.hasNext();){
//remove edge e from the graph
Edge e = it.next();
Task m = e.to;
it.remove();//Remove edge from n
m.inEdges.remove(e);//Remove edge from m
//if m has no other incoming edges then insert m into S
if(m.inEdges.isEmpty()){
G.S.add(m);
}//if
}//for
} //尽管
Task 类扩展了 Thread 类,每个 Task 都有一个 inEdges 和 outEdges 的 HashSet。我应该如何处理?