source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/docs/manual/CoreTasks/parallel.html@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 7.7 KB
Line 
1<html>
2
3<head>
4<meta http-equiv="Content-Language" content="en-us">
5<title>Parallel Task</title>
6<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
7</head>
8
9<body>
10
11<h2>Parallel</h2>
12<h3>Description</h3>
13<p>Parallel is a container task - it can contain other Ant tasks. Each nested
14task within the parallel task will be executed in its own thread. </p>
15<h3>Parameters</h3>
16<table border="1" cellpadding="2" cellspacing="0">
17 <tr>
18 <td valign="top"><b>Attribute</b></td>
19 <td valign="top"><b>Description</b></td>
20 <td align="center" valign="top"><b>Required</b></td>
21 </tr>
22 <tr>
23 <td valign="top">threadCount</td>
24 <td valign="top">Maximum numbers of thread to use.</td>
25 <td align="center" valign="top">No</td>
26 </tr>
27 <tr>
28 <td valign="top">threadsPerProcessor</td>
29 <td valign="top">Maximum number of threads to use per available processor
30(Requires JDK 1.4)</td>
31 <td align="center" valign="top">No, defers to threadCount</td>
32 </tr>
33 <tr>
34 <td valign="top">pollInterval</td>
35 <td valign="top">Currently has no effect</td>
36 <td align="center" valign="top">No, default is 1000</td>
37 </tr>
38 <tr>
39 <td valign="top">timeout</td>
40 <td valign="top">Number of milliseconds before execution is terminated</td>
41 <td align="center" valign="top">No</td>
42 </tr>
43 <tr>
44 <td valign="top">failonany</td>
45 <td valign="top">If any of the nested tasks fails, execution of the task completes
46 at that point without waiting for any other tasks to complete.</td>
47 <td align="center" valign="top">No</td>
48 </tr>
49</table>
50
51<p>Parallel tasks have a number of uses in an Ant build file including:</p>
52<ul>
53<li>Taking advantage of available processing resources to reduce build time</li>
54<li>Testing servers, where the server can be run in one thread and the test
55harness is run in another thread.</li>
56</ul>
57
58<p>Care must be taken when using multithreading to ensure the tasks within the
59threads do not interact. For example, two javac compile tasks which write
60classes into the same destination directory may interact where one tries to
61read a class for dependency information while the other task is writing the
62class file. Be sure to avoid these types of interactions within a
63<code>&lt;parallel&gt;</code> task</p>
64
65<p>Any valid Ant task may be embedded within a
66parallel task, including other parallel tasks.</p>
67
68<p>Note that while the tasks within the parallel task are being run, the main
69thread will be blocked waiting for all the child threads to complete. If
70execution is terminated by a timeout or a nested task failure when the failonany
71flag is set, the parallel task will complete without waiting for other nested
72tasks to complete in other threads.
73</p>
74
75<p>If any of the tasks within the <code>&lt;parallel&gt;</code> task fails and failonany is
76not set, the remaining tasks in other threads will continue to run until
77all threads have completed. In this situation, the parallel task will also fail.</p>
78
79<p>The parallel task may be combined with the <a href="sequential.html">
80sequential</a> task to define sequences of tasks to be executed on each thread
81within the parallel block</p>
82
83<p>The threadCount attribute can be used to place a maximum number of available
84threads for the execution. When not present all child tasks will be executed at
85once. When present then the maximum number of concurrently executing tasks will
86not exceed the number of threads specified. Furthermore, each task will be
87started in the order they are given. But no guarantee is made as to the speed
88of execution or the order of completion of the tasks, only that each will be
89started before the next.<p>
90
91<p>If you are using J2RE 1.4 or later you can also use the threadsPerProcessor
92and the number of available threads will be the stated multiple of the number of
93processors (there is no affinity to a particular processor however). This will
94override the value in threadCount. If threadsPerProcessor is specified using
95any version prior to 1.4 then the value in threadCount will be used as is.</p>
96
97<p>When using threadCount and threadsPerProcessor care should be taken to ensure
98that the build does not deadlock. This can be caused by tasks such as waitFor
99taking up all available threads before the tasks that would unlock the waitfor
100would occur. This is not a repalcement for Java Language level thread
101semantics and is best used for "embarassingly parallel" tasks.</p>
102
103
104<h3>Parameters specified as nested elements</h3>
105
106<h4>daemons</h4>
107<p>
108The parallel task supports a <code>&lt;daemons&gt;</code> nested element. This is a list of tasks
109which are to be run in parallel daemon threads. The parallel task will not wait for
110these tasks to complete. Being daemon threads, however, they will not prevent Ant from
111completing, whereupon the threads are terminated. Failures in daemon threads which
112occur before the parallel task itself finishes will be reported and can cause
113parallel to throw an exception. Failures which occur after parallel has completed are not
114reported.
115</p>
116
117<p>Daemon tasks can be used, for example, to start test servers which might not be easily
118terminated from Ant. By using <code>&lt;daemons&gt;</code> such servers do not halt the build.
119</p>
120
121
122<h3>Examples</h3>
123<pre>
124&lt;parallel&gt;
125 &lt;wlrun ... &gt;
126 &lt;sequential&gt;
127 &lt;sleep seconds=&quot;30&quot;/&gt;
128 &lt;junit ... &gt;
129 &lt;wlstop/&gt;
130 &lt;/sequential&gt;
131&lt;/parallel&gt;
132</pre>
133<p>This example represents a typical pattern for testing a server application.
134In one thread the server is started (the wlrun task). The other thread consists
135of a three tasks which are performed in sequence. The sleep task is used to
136give the server time to come up. Another task which is capable of validating
137that the server is available could be used in place of the sleep task. The
138test harness is then run. Once the tests are complete, the server is stopped
139(using wlstop in this example), allowing both threads to complete. The
140parallel task will also complete at this time and the build will then
141continue.</p>
142
143<pre>
144&lt;parallel&gt;
145 &lt;javac ...&gt; &lt;!-- compiler servlet code --&gt;
146 &lt;wljspc ...&gt; &lt;!-- precompile JSPs --&gt;
147&lt;/parallel&gt;
148</pre>
149
150<p>This example shows two independent tasks being run to achieve better
151resource utilization during the build. In this instance, some servlets are being
152compiled in one thead and a set of JSPs is being precompiled in another. As
153noted above, you need to be careful that the two tasks are independent, both in
154terms of their dependencies and in terms of their potential interactions in
155Ant's external environment.</p>
156
157<pre>
158&lt;parallel threadCount='4'&gt;
159 &lt;ant target='TargetThatConsumesLotsOfCPUTimeAndMemory'&gt;
160 &lt;param name='file' value='one.txt'/&gt;
161 &lt;/ant&gt;
162 &lt;ant target='TargetThatConsumesLotsOfCPUTimeAndMemory'&gt;
163 &lt;param name='file' value='two.txt'/&gt;
164 &lt;/ant&gt;
165 &lt;ant target='TargetThatConsumesLotsOfCPUTimeAndMemory'&gt;
166 &lt;param name='file' value='three.txt'/&gt;
167 &lt;/ant&gt;
168 &lt;!-- repeated about 40 times --&gt;
169&lt;/parallel&gt;
170</pre>
171
172<p>This example represents a typical need for use of the threadCount and
173threadsPerProcessor attributes. Spinning up all 40 of those tasks could cripple
174the JVM for memory and the CPU for available time. By limiting the number of
175concurrent executions you can get the task done in about the same assuming
176infinite memory time without needing infinite memory. This is also a good
177candidiate for use of threadCount (and possibly threadsPerProcessor) because
178each task (in this hypothetical case) is independent and has no dependencies on
179the other tasks.</p>
180
181<hr>
182<p align="center">Copyright &copy; 2001-2004 The Apache Software Foundation. All rights
183Reserved.</p>
184</body>
185</html>
186
Note: See TracBrowser for help on using the repository browser.