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

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