1 | package felix.society; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.HashSet; |
5 | import java.util.concurrent.ExecutorService; |
6 | import java.util.concurrent.Future; |
7 | |
8 | import felix.society.Task.TaskType; |
9 | import felix.society.TaskList.TaskListExecutionWorker; |
10 | |
11 | /** |
12 | * TaskSet -- Subtasks in it can be executed in parallel |
13 | * (and then joined). |
14 | * |
15 | * |
16 | * @author czhang |
17 | * |
18 | */ |
19 | public class TaskSet extends Task{ |
20 | |
21 | public TaskSet(){ |
22 | super(TaskType.TASKSET); |
23 | } |
24 | |
25 | public TaskSet(Integer _nRuns){ |
26 | super(TaskType.TASKSET, _nRuns); |
27 | } |
28 | |
29 | @Override |
30 | public Worker generateWorker() { |
31 | return null; |
32 | } |
33 | |
34 | public Worker generateWorker(ExecutorService pool) { |
35 | return new TaskSetExecutionWorker(this.subTasks, pool); |
36 | } |
37 | |
38 | /** |
39 | * Worker for running this TaskSet. |
40 | * @author czhang |
41 | * |
42 | */ |
43 | public class TaskSetExecutionWorker extends Worker{ |
44 | |
45 | ArrayList<Task> subtasks; |
46 | ExecutorService pool; |
47 | |
48 | public TaskSetExecutionWorker(ArrayList<Task> _subtasks, |
49 | ExecutorService _pool){ |
50 | subtasks = _subtasks; |
51 | pool = _pool; |
52 | } |
53 | |
54 | @Override |
55 | public void run() { |
56 | |
57 | try{ |
58 | |
59 | ArrayList<Future> futures = new ArrayList<Future>(); |
60 | |
61 | for(Task t : subTasks){ |
62 | if(t.getType() == TaskType.TASK){ |
63 | currentWorker = t.generateWorker(); |
64 | currentFuture = pool.submit(currentWorker); |
65 | futures.add(currentFuture); |
66 | }else{ |
67 | currentWorker = t.generateWorker(pool); |
68 | currentFuture = softpool.submit(currentWorker); |
69 | futures.add(currentFuture); |
70 | } |
71 | } |
72 | |
73 | for(Future f : futures){ |
74 | f.get(); |
75 | } |
76 | |
77 | }catch(Exception e){ |
78 | submitException(e); |
79 | } |
80 | } |
81 | |
82 | } |
83 | |
84 | } |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |