1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.api.runorder;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27 public class ThreadedExecutionScheduler {
28 private final int numThreads;
29
30 private final int runTime[];
31
32 private final List<Class<?>>[] lists;
33
34 @SuppressWarnings("unchecked")
35 public ThreadedExecutionScheduler(int numThreads) {
36 this.numThreads = numThreads;
37 runTime = new int[numThreads];
38 lists = new List[numThreads];
39 for (int i = 0; i < numThreads; i++) {
40 lists[i] = new ArrayList<>();
41 }
42 }
43
44 public void addTest(PrioritizedTest prioritizedTest) {
45 final int leastBusySlot = findLeastBusySlot();
46 runTime[leastBusySlot] += prioritizedTest.getTotalRuntime();
47
48 lists[leastBusySlot].add(prioritizedTest.getClazz());
49 }
50
51 public List<Class<?>> getResult() {
52 List<Class<?>> result = new ArrayList<>();
53 int index = 0;
54 boolean added;
55 do {
56 added = false;
57 for (int i = 0; i < numThreads; i++) {
58 if (lists[i].size() > index) {
59 result.add(lists[i].get(index));
60 added = true;
61 }
62 }
63 index++;
64 } while (added);
65 return result;
66 }
67
68 private int findLeastBusySlot() {
69 int leastBusy = 0;
70 int minRuntime = runTime[0];
71 for (int i = 1; i < numThreads; i++) {
72 if (runTime[i] < minRuntime) {
73 leastBusy = i;
74 minRuntime = runTime[i];
75 }
76 }
77 return leastBusy;
78 }
79 }