You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

parallel.html 9.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Parallel Task</title>
  20. </head>
  21. <body>
  22. <h2>Parallel</h2>
  23. <h3>Description</h3>
  24. <p>
  25. Executes nested tasks in parallel with no guarantees of thread safety.
  26. Every task will run in its own thread, with the likelihood of
  27. concurrency problems scaling with the number of CPUs on the host system.
  28. </p>
  29. <p><b>Warning:</b> While the Ant core is believed to be thread safe, no such
  30. guarantees are made about tasks, which are not tested for thread safety during
  31. Ant's test process.
  32. Third party tasks may or may not be thread safe, and some of Ant's core tasks, such as
  33. <code>&lt;javac&gt;</code> are definitely not re-entrant. This is because they use libraries that
  34. were never designed to be used in a multithreaded environment.
  35. </p>
  36. <p>
  37. The primary use case for <code>&lt;parallel&gt;</code> is to run external programs
  38. such as an application server, and the JUnit or TestNG test suites at the
  39. same time. Anyone trying to run large Ant task sequences in parallel, such
  40. as javadoc and javac at the same time, is implicitly taking on the task
  41. of identifying and fixing all concurrency bugs the tasks that they run.
  42. </p>
  43. <p>
  44. Accordingly, while this task has uses, it should be considered an advanced
  45. task which should be used in certain batch-processing or testing situations,
  46. rather than an easy trick to speed up build times on a multiway CPU.
  47. </p>
  48. <h3>Parameters</h3>
  49. <table border="1" cellpadding="2" cellspacing="0">
  50. <tr>
  51. <td valign="top"><b>Attribute</b></td>
  52. <td valign="top"><b>Description</b></td>
  53. <td align="center" valign="top"><b>Required</b></td>
  54. </tr>
  55. <tr>
  56. <td valign="top">threadCount</td>
  57. <td valign="top">Maximum numbers of thread to use.</td>
  58. <td align="center" valign="top">No</td>
  59. </tr>
  60. <tr>
  61. <td valign="top">threadsPerProcessor</td>
  62. <td valign="top">Maximum number of threads to use per available processor
  63. (Java 1.4+)</td>
  64. <td align="center" valign="top">No, defers to threadCount</td>
  65. </tr>
  66. <tr>
  67. <td valign="top">timeout</td>
  68. <td valign="top">Number of milliseconds before execution is terminated</td>
  69. <td align="center" valign="top">No</td>
  70. </tr>
  71. <tr>
  72. <td valign="top">failonany</td>
  73. <td valign="top">If any of the nested tasks fails, execution of the task completes
  74. at that point without waiting for any other tasks to complete.</td>
  75. <td align="center" valign="top">No</td>
  76. </tr>
  77. <tr>
  78. <td valign="top">pollInterval</td>
  79. <td valign="top">Currently has no effect</td>
  80. <td align="center" valign="top">No, default is 1000</td>
  81. </tr>
  82. </table>
  83. <p>Parallel tasks have a number of uses in an Ant build file including:</p>
  84. <ul>
  85. <li>Taking advantage of available processing resources to execute external
  86. programs simultaneously.</li>
  87. <li>Testing servers, where the server can be run in one thread and the test
  88. harness is run in another thread.</li>
  89. </ul>
  90. <p>Any valid Ant task may be embedded within a
  91. parallel task, including other parallel tasks, though there is no guarantee that
  92. the tasks will be thread safe in such an environment.</p>
  93. <p>While the tasks within the parallel task are being run, the main
  94. thread will be blocked waiting for all the child threads to complete. If
  95. execution is terminated by a timeout or a nested task failure when the
  96. <code>failonany</code>
  97. flag is set, the parallel task will complete without waiting for other nested
  98. tasks to complete in other threads.
  99. </p>
  100. <p>If any of the tasks within the <code>&lt;parallel&gt;</code> task fails and failonany is
  101. not set, the remaining tasks in other threads will continue to run until
  102. all threads have completed. In this situation, the parallel task will also fail.</p>
  103. <p>The parallel task may be combined with the <a href="sequential.html">
  104. sequential</a> task to define sequences of tasks to be executed on each thread
  105. within the parallel block</p>
  106. <p>The <code>threadCount</code> attribute can be used to place a maximum number of available
  107. threads for the execution. When not present all child tasks will be executed at
  108. once. When present then the maximum number of concurrently executing tasks will
  109. not exceed the number of threads specified. Furthermore, each task will be
  110. started in the order they are given. But no guarantee is made as to the speed
  111. of execution or the order of completion of the tasks, only that each will be
  112. started before the next.<p>
  113. <p>If you are using Java 1.4 or later you can also use the <code>threadsPerProcessor</code>
  114. and the number of available threads will be the stated multiple of the number of
  115. processors (there is no affinity to a particular processor however). This will
  116. override the value in <code>threadCount</code>. If <code>threadsPerProcessor</code>
  117. is specified on any older JVM, then the value in <code>threadCount</code> will be used as is.</p>
  118. <p>When using <code>threadCount</code> and <code>threadsPerProcessor</code>
  119. care should be taken to ensure that the build does not deadlock.
  120. This can be caused by tasks such as <code>waitfor</code>
  121. taking up all available threads before the tasks that would unlock the
  122. <code>waitfor</code>
  123. would occur. This is not a replacement for Java Language level thread
  124. semantics and is best used for "embarassingly parallel" tasks.</p>
  125. <h3>Parameters specified as nested elements</h3>
  126. <h4>daemons</h4>
  127. <p>
  128. The parallel task supports a <code>&lt;daemons&gt;</code> nested element. This is a list of tasks
  129. which are to be run in parallel daemon threads. The parallel task will not wait for
  130. these tasks to complete. Being daemon threads, however, they will not prevent Ant from
  131. completing, whereupon the threads are terminated. Failures in daemon threads which
  132. occur before the parallel task itself finishes will be reported and can cause
  133. parallel to throw an exception. Failures which occur after parallel has completed are not
  134. reported.
  135. </p>
  136. <p>Daemon tasks can be used, for example, to start test servers which might not be easily
  137. terminated from Ant. By using <code>&lt;daemons&gt;</code> such servers do not halt the build.
  138. </p>
  139. <h3>Examples</h3>
  140. <pre>
  141. &lt;parallel&gt;
  142. &lt;wlrun ... &gt;
  143. &lt;sequential&gt;
  144. &lt;sleep seconds=&quot;30&quot;/&gt;
  145. &lt;junit fork="true" forkmode="once" ... &gt;
  146. &lt;wlstop/&gt;
  147. &lt;/sequential&gt;
  148. &lt;/parallel&gt;
  149. </pre>
  150. <p>This example represents a typical pattern for testing a server application.
  151. In one thread the server is started (the <code>&lt;wlrun&gt;</code> task).
  152. The other thread consists
  153. of a three tasks which are performed in sequence. The <code>&lt;sleep&gt;</code> task is used to
  154. give the server time to come up. Another task which is capable of validating
  155. that the server is available could be used in place of the <code>&lt;sleep&gt;</code> task. The
  156. <code>&lt;junit&gt;</code> test harness then runs, again in its own JVM. Once the tests are complete, the server is stopped
  157. (using <code>&lt;wlstop&gt;</code> in this example), allowing both threads to complete. The
  158. <code>&lt;parallel&gt;</code> task will also complete at this time and the build will then
  159. continue.</p>
  160. <pre>
  161. &lt;parallel&gt;
  162. &lt;javac fork="true"...&gt; &lt;!-- compiler servlet code --&gt;
  163. &lt;wljspc ...&gt; &lt;!-- precompile JSPs --&gt;
  164. &lt;/parallel&gt;
  165. </pre>
  166. <p>This example shows two independent tasks being run to achieve better
  167. resource utilization during the build. In this instance, some servlets are being
  168. compiled in one thead and a set of JSPs is being precompiled in another. Developers
  169. need to be careful that the two tasks are independent, both in
  170. terms of their dependencies and in terms of their potential interactions in
  171. Ant's external environment. Here we set <code>fork="true"</code> for the
  172. <code>&lt;javac&gt;</code> task, so that it runs in a new process;
  173. if the <code>&lt;wljspc&gt;</code> task used the javac compiler in-VM
  174. (it may), concurrency problems may arise.
  175. </p>
  176. <taskdef name=""
  177. <pre>
  178. &lt;macrodef name="dbpurge"&gt;
  179. &lt;attribute file="file"/&gt;
  180. &lt;sequential&gt;
  181. &lt;java jar="utils/dbpurge.jar" fork="true" &gt;
  182. &lt;arg file="@{file} /&gt;
  183. &lt;/java&gt;
  184. &lt;/sequential&gt;
  185. &lt;/macrodef&gt;
  186. &lt;parallel threadCount='4'&gt;
  187. &lt;dbpurge file="db/one" /&gt;
  188. &lt;dbpurge file="db/two" /&gt;
  189. &lt;dbpurge file="db/three" /&gt;
  190. &lt;dbpurge file="db/four" /&gt;
  191. &lt;dbpurge file="db/five" /&gt;
  192. &lt;dbpurge file="db/six" /&gt;
  193. &lt;dbpurge file="db/seven" /&gt;
  194. &lt;dbpurge file="db/eight" /&gt;
  195. &lt;!-- repeated about 40 times --&gt;
  196. &lt;/parallel&gt;
  197. </pre>
  198. <p>This example represents a typical need for use of the threadCount and
  199. threadsPerProcessor attributes. Spinning up all 40 of those tasks could cripple
  200. the system for memory and CPU time. By limiting the number of
  201. concurrent executions you can reduce contention for CPU, memory and disk IO,
  202. and so actually finish faster. This is also a good
  203. candidiate for use of threadCount (and possibly threadsPerProcessor) because
  204. each task is independent (every new JVM is forked) and has no dependencies on
  205. the other tasks.</p>
  206. </body>
  207. </html>