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

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