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 8.3 KiB

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