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.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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>Executes nested tasks in parallel with no guarantees of thread safety. Every task will run in
  25. its own thread, with the likelihood of concurrency problems scaling with the number of CPUs on the
  26. host system.</p>
  27. <p><strong>Warning</strong>: While the Apache Ant core is believed to be thread safe, no such
  28. guarantees are made about tasks, which are not tested for thread safety during Ant's test process.
  29. Third party tasks may or may not be thread safe, and some of Ant's core tasks, such
  30. as <code>&lt;javac&gt;</code> are definitely not re-entrant. This is because they use libraries that
  31. were never designed to be used in a multithreaded environment.</p>
  32. <p>The primary use case for <code>&lt;parallel&gt;</code> is to run external programs such as an
  33. application server, and the JUnit or TestNG test suites at the same time. Anyone trying to run large
  34. Ant task sequences in parallel, such as <code>javadoc</code> and <code>javac</code> at the same
  35. time, is implicitly taking on the task of identifying and fixing all concurrency bugs the tasks that
  36. they run.</p>
  37. <p>Accordingly, while this task has uses, it should be considered an advanced task which should be
  38. used in certain batch processing or testing situations, rather than an easy trick to speed up build
  39. times on a multicore CPU.</p>
  40. <h3>Parameters</h3>
  41. <table class="attr">
  42. <tr>
  43. <th>Attribute</th>
  44. <th>Description</th>
  45. <th>Required</th>
  46. </tr>
  47. <tr>
  48. <td>threadCount</td>
  49. <td>Maximum numbers of thread to use.</td>
  50. <td>No</td>
  51. </tr>
  52. <tr>
  53. <td>threadsPerProcessor</td>
  54. <td>Maximum number of threads to use per available processor (Java 1.4+)</td>
  55. <td>No; defers to <var>threadCount</var></td>
  56. </tr>
  57. <tr>
  58. <td>timeout</td>
  59. <td>Number of milliseconds before execution is terminated</td>
  60. <td>No</td>
  61. </tr>
  62. <tr>
  63. <td>failonany</td>
  64. <td>If any of the nested tasks fails, execution of the task completes at that point without
  65. waiting for any other tasks to complete.</td>
  66. <td>No; default is <q>false</q>.</td>
  67. </tr>
  68. <tr>
  69. <td>pollInterval</td>
  70. <td>Currently has no effect</td>
  71. <td>No; default is <q>1000</q></td>
  72. </tr>
  73. </table>
  74. <p>Parallel tasks have a number of uses in an Ant build file including:</p>
  75. <ul>
  76. <li>Taking advantage of available processing resources to execute external programs
  77. simultaneously.</li>
  78. <li>Testing servers, where the server can be run in one thread and the test harness is run in
  79. another thread.</li>
  80. </ul>
  81. <p>Any valid Ant task may be embedded within a <code>parallel</code> task, including
  82. other <code>parallel</code> tasks, though there is no guarantee that the tasks will be thread safe
  83. in such an environment.</p>
  84. <p>While the tasks within the <code>parallel</code> task are being run, the main thread will be
  85. blocked waiting for all the child threads to complete. If execution is terminated by a timeout or a
  86. nested task failure when the <var>failonany</var> flag is set, the <code>parallel</code> task will
  87. complete without waiting for other nested tasks to complete in other threads.</p>
  88. <p>If any of the tasks within the <code>&lt;parallel&gt;</code> task fails and <var>failonany</var>
  89. is not set, the remaining tasks in other threads will continue to run until all threads have
  90. completed. In this situation, the <code>parallel</code> task will also fail.</p>
  91. <p>The <code>parallel</code> task may be combined with the <a href="sequential.html">sequential</a>
  92. task to define sequences of tasks to be executed on each thread within the parallel block.</p>
  93. <p>The <var>threadCount</var> attribute can be used to place a maximum number of available threads
  94. for the execution. When not present all child tasks will be executed at once. When present then
  95. the maximum number of concurrently executing tasks will not exceed the number of threads specified.
  96. Furthermore, each task will be started in the order they are given. But no guarantee is made as to
  97. the speed of execution or the order of completion of the tasks, only that each will be started
  98. before the next.<p>
  99. <p>If you are using Java 1.4 or later you can also use the <var>threadsPerProcessor</var> and the
  100. number of available threads will be the started multiple of the number of processors (there is no
  101. affinity to a particular processor, however). This will override the value
  102. in <var>threadCount</var>. If <var>threadsPerProcessor</var> is specified on any older JVM, then
  103. the value in <var>threadCount</var> will be used as is.</p>
  104. <p>When using <var>threadCount</var> and <var>threadsPerProcessor</var> care should be taken to
  105. ensure that the build does not deadlock. This can be caused by tasks such as <code>waitfor</code>
  106. taking up all available threads before the tasks that would unlock the <code>waitfor</code> would
  107. occur. This is not a replacement for Java Language level thread semantics and is best used for
  108. "embarrassingly parallel" tasks.</p>
  109. <h3>Parameters specified as nested elements</h3>
  110. <h4>daemons</h4>
  111. <p>The <code>parallel</code> task supports a <code>&lt;daemons&gt;</code> nested element. This is a
  112. list of tasks which are to be run in parallel daemon threads. The <code>parallel</code> task will
  113. not wait for these tasks to complete. Being daemon threads, however, they will not prevent Ant from
  114. completing, whereupon the threads are terminated. Failures in daemon threads which occur before
  115. the <code>parallel</code> task itself finishes will be reported and can cause <code>parallel</code>
  116. to throw an exception. Failures which occur after <code>parallel</code> has completed are not
  117. reported.</p>
  118. <p>Daemon tasks can be used, for example, to start test servers which might not be easily terminated
  119. from Ant. By using <code>&lt;daemons&gt;</code> such servers do not halt the build.</p>
  120. <h3>Examples</h3>
  121. <pre>
  122. &lt;parallel&gt;
  123. &lt;wlrun ... &gt;
  124. &lt;sequential&gt;
  125. &lt;sleep seconds=&quot;30&quot;/&gt;
  126. &lt;junit fork="true" forkmode="once" ... &gt;
  127. &lt;wlstop/&gt;
  128. &lt;/sequential&gt;
  129. &lt;/parallel&gt;</pre>
  130. <p>This example represents a typical pattern for testing a server application. In one thread the
  131. server is started (the <code>&lt;wlrun&gt;</code> task). The other thread consists of a three tasks
  132. which are performed in sequence. The <code>&lt;sleep&gt;</code> task is used to give the server time
  133. to come up. Another task which is capable of validating that the server is available could be used
  134. in place of the <code>&lt;sleep&gt;</code> task. The <code>&lt;junit&gt;</code> test harness then
  135. runs, again in its own JVM. Once the tests are complete, the server is stopped
  136. (using <code>&lt;wlstop&gt;</code> in this example), allowing both threads to complete. The
  137. <code>&lt;parallel&gt;</code> task will also complete at this time and the build will then
  138. continue.</p>
  139. <pre>
  140. &lt;parallel&gt;
  141. &lt;javac fork="true"...&gt; &lt;!-- compiler servlet code --&gt;
  142. &lt;wljspc ...&gt; &lt;!-- precompile JSPs --&gt;
  143. &lt;/parallel&gt;</pre>
  144. <p>This example shows two independent tasks being run to achieve better resource utilization during
  145. the build. In this instance, some servlets are being compiled in one thread and a set of JSPs is
  146. being precompiled in another. Developers need to be careful that the two tasks are independent, both
  147. in terms of their dependencies and in terms of their potential interactions in Ant's external
  148. environment. Here we set <var>fork</var>=<q>true</q> for the <code>&lt;javac&gt;</code> task, so
  149. that it runs in a new process; if the <code>&lt;wljspc&gt;</code> task used the <kbd>javac</kbd>
  150. compiler in-VM (it may), concurrency problems may arise.</p>
  151. <pre>
  152. &lt;macrodef name="dbpurge"&gt;
  153. &lt;attribute file="file"/&gt;
  154. &lt;sequential&gt;
  155. &lt;java jar="utils/dbpurge.jar" fork="true" &gt;
  156. &lt;arg file="@{file}/&gt;
  157. &lt;/java&gt;
  158. &lt;/sequential&gt;
  159. &lt;/macrodef&gt;
  160. &lt;parallel threadCount="4"&gt;
  161. &lt;dbpurge file="db/one"/&gt;
  162. &lt;dbpurge file="db/two"/&gt;
  163. &lt;dbpurge file="db/three"/&gt;
  164. &lt;dbpurge file="db/four"/&gt;
  165. &lt;dbpurge file="db/five"/&gt;
  166. &lt;dbpurge file="db/six"/&gt;
  167. &lt;dbpurge file="db/seven"/&gt;
  168. &lt;dbpurge file="db/eight"/&gt;
  169. &lt;!-- repeated about 40 times --&gt;
  170. &lt;/parallel&gt;</pre>
  171. <p>This example represents a typical need for use of the <var>threadCount</var>
  172. and <var>threadsPerProcessor</var> attributes. Spinning up all 40 of those tasks could cripple the
  173. system for memory and CPU time. By limiting the number of concurrent executions you can reduce
  174. contention for CPU, memory and disk IO, and so actually finish faster. This is also a good
  175. candidate for use of <var>threadCount</var> (and possibly <var>threadsPerProcessor</var>) because
  176. each task is independent (every new JVM is forked) and has no dependencies on the other tasks.</p>
  177. </body>
  178. </html>