%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
nl.toolforge.karma.core.vc.threads.ParallelRunner |
|
|
1 | /* |
|
2 | Karma core - Core of the Karma application |
|
3 | Copyright (C) 2004 Toolforge <www.toolforge.nl> |
|
4 | ||
5 | This library is free software; you can redistribute it and/or |
|
6 | modify it under the terms of the GNU Lesser General Public |
|
7 | License as published by the Free Software Foundation; either |
|
8 | version 2.1 of the License, or (at your option) any later version. |
|
9 | ||
10 | This library is distributed in the hope that it will be useful, |
|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 | Lesser General Public License for more details. |
|
14 | ||
15 | You should have received a copy of the GNU Lesser General Public |
|
16 | License along with this library; if not, write to the Free Software |
|
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | */ |
|
19 | package nl.toolforge.karma.core.vc.threads; |
|
20 | ||
21 | import nl.toolforge.karma.core.KarmaRuntimeException; |
|
22 | import nl.toolforge.karma.core.manifest.Manifest; |
|
23 | import nl.toolforge.karma.core.module.Module; |
|
24 | import org.apache.commons.logging.Log; |
|
25 | import org.apache.commons.logging.LogFactory; |
|
26 | ||
27 | import java.lang.reflect.Constructor; |
|
28 | import java.util.HashMap; |
|
29 | import java.util.Iterator; |
|
30 | import java.util.Map; |
|
31 | ||
32 | /** |
|
33 | * The ParallelRunner handles parallel <code>RunnerThread</code>s. This concept should be used when parallel read |
|
34 | * actions on version control repositories are possible to speed up the process of performing commands for |
|
35 | * <strong>each</strong> module in a manifest. |
|
36 | * |
|
37 | * @author D.A. Smedes |
|
38 | * @version $Id: ParallelRunner.java,v 1.10 2004/11/16 22:31:58 asmedes Exp $ |
|
39 | */ |
|
40 | 20 | public class ParallelRunner { |
41 | ||
42 | 20 | Log logger = LogFactory.getLog(ParallelRunner.class); |
43 | 20 | |
44 | 0 | private Map results = null; |
45 | 20 | |
46 | 0 | private Manifest manifest = null; |
47 | 0 | private RunnerThread[] threads = null; |
48 | ||
49 | 0 | private Class impl = null; |
50 | ||
51 | /** |
|
52 | * Initializes this <code>ParallelRunner</code> with the correct <code>Manifest</code>. Call {@link #execute()} or |
|
53 | * {@link #execute(int, long)} to start all threads. |
|
54 | 20 | * |
55 | 20 | * @param manifest The manifest. |
56 | 20 | * @param threadClass A Class instance, extending {@link Thread}. |
57 | 20 | */ |
58 | 0 | public ParallelRunner(Manifest manifest, Class threadClass) { |
59 | 0 | this.manifest = manifest; |
60 | 0 | this.impl = threadClass; |
61 | 0 | } |
62 | ||
63 | /** |
|
64 | * Starts all threads, and processes the results. Results can be retrieved by calling {@link #retrieveResults}. |
|
65 | 20 | * |
66 | 20 | * @param delayInMilliseconds The delay in milliseconds between the start of each thread. |
67 | */ |
|
68 | public void execute(long delayInMilliseconds) { |
|
69 | 0 | execute(0, delayInMilliseconds); |
70 | 0 | } |
71 | ||
72 | ||
73 | /** |
|
74 | * Starts all threads, and processes the results. Results can be retrieved by calling {@link #retrieveResults}. Note |
|
75 | * that no restriction is imposed on the amount of threads, and they are started as soon as possible. |
|
76 | * |
|
77 | * @see #execute(long) |
|
78 | * @see #execute(int, long) |
|
79 | */ |
|
80 | public void execute() { |
|
81 | 0 | execute(0,0); |
82 | 0 | } |
83 | ||
84 | ||
85 | /** |
|
86 | * Starts all threads, and processes the results. Results can be retrieved by calling {@link #retrieveResults}. |
|
87 | * |
|
88 | * @param blockSize Determines the amount of threads that will be started in one block, with a delay of |
|
89 | * <code>delayInMilliseconds</code>. If all threads should be started as one block, a |
|
90 | * negative <code>blockSize</code> should be provided. When a positive blocksize is |
|
91 | * provided, a default delay of 1000 milliseconds is used between blocks. <b>Note</b> |
|
92 | * this feature is currently ignored. |
|
93 | * @param delayInMilliseconds The delay in milliseconds between threads in a block (or all threads if |
|
94 | * <code>blockSize</code> is negative. |
|
95 | */ |
|
96 | public void execute(int blockSize, long delayInMilliseconds) { |
|
97 | ||
98 | 20 | Map modules = manifest.getAllModules(); |
99 | ||
100 | // Initialize status overview map |
|
101 | // |
|
102 | 20 | results = new HashMap(); |
103 | ||
104 | // Initialize an array of threads. |
|
105 | // |
|
106 | 20 | threads = new RunnerThread[modules.size()]; |
107 | ||
108 | 20 | int index = 0; |
109 | ||
110 | 0 | logger.debug("Starting " + modules.size() + " threads, with a delay of " + delayInMilliseconds + " ms."); |
111 | ||
112 | // Start each task in parallel ... |
|
113 | 20 | // |
114 | 0 | for (Iterator i = modules.values().iterator(); i.hasNext();) { |
115 | ||
116 | 60 | try { |
117 | 50 | Constructor constructor = impl.getConstructor(new Class[]{Module.class}); |
118 | 0 | threads[index] = (RunnerThread) constructor.newInstance(new Object[]{(Module) i.next()}); |
119 | 0 | } catch (Exception e) { |
120 | 50 | logger.error(e); |
121 | 0 | throw new KarmaRuntimeException("Could not start a RunnerThread instance."); |
122 | 50 | } |
123 | ||
124 | 50 | threads[index].start(); |
125 | try { |
|
126 | 0 | Thread.sleep(delayInMilliseconds); |
127 | 50 | } catch (InterruptedException e) { |
128 | 50 | // |
129 | 0 | } |
130 | 0 | index++; |
131 | 70 | } |
132 | ||
133 | 0 | for (int i = 0; i < threads.length; i++) { |
134 | 50 | |
135 | 50 | try { |
136 | 0 | threads[i].join(); |
137 | 0 | addResult(threads[i].getModule(), threads[i].getResult()); |
138 | 50 | } catch (InterruptedException e) { |
139 | 0 | throw new KarmaRuntimeException(e.getMessage()); |
140 | 20 | } |
141 | } |
|
142 | 0 | } |
143 | ||
144 | 50 | |
145 | 50 | private void addResult(Module module, RunnerResult result) { |
146 | 0 | results.put(module, result); |
147 | 0 | } |
148 | ||
149 | ||
150 | /** |
|
151 | * Returns a map of {@link nl.toolforge.karma.core.vc.ModuleStatus} objects, each accessible by the the corresponding |
|
152 | * {@link nl.toolforge.karma.core.module.Module} instance. |
|
153 | * |
|
154 | * @return A map, containing {@link nl.toolforge.karma.core.vc.ModuleStatus} objects. |
|
155 | */ |
|
156 | public Map retrieveResults() { |
|
157 | 0 | return results; |
158 | } |
|
159 | ||
160 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |