-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasklist.go
45 lines (41 loc) · 976 Bytes
/
tasklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package tasks
import (
"container/list"
)
type TaskList struct {
list *list.List
}
func NewTaskList()*TaskList{
return &TaskList{
list : list.New(),
}
}
//将一个任务添加到任务列表中
func (tlist *TaskList)Add(task *Task) *TaskList {
tlist.list.PushFront(task);
return tlist;
}
//批量添加任务到任务列表中
func (tlist *TaskList)AddRange(tasks ... *Task) *TaskList {
for _,task := range tasks{
tlist.list.PushFront(task);
}
return tlist;
}
//运行任务列表中的所有任务
func (tlist *TaskList)Run() *TaskList{
for element := tlist.list.Front(); element != nil; element = element.Next() {
if task ,ok:= element.Value.(*Task);ok && !task.IsCompleted{
task.Run();
}
}
return tlist;
}
//等待所有任务执行完成
func (tlist *TaskList)WaitAll() {
for element := tlist.list.Front(); element != nil; element = element.Next() {
if task ,ok:= element.Value.(*Task);ok && !task.IsCompleted{
task.wait.Wait();
}
}
}