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