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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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">Maximum number of milliseconds to wait for before checking
  32. when waiting for available threads.</td>
  33. <td align="center" valign="top">No, default is 1000</td>
  34. </tr>
  35. </table>
  36. <p>Parallel tasks have a number of uses in an Ant build file including:</p>
  37. <ul>
  38. <li>Taking advantage of available processing resources to reduce build time</li>
  39. <li>Testing servers, where the server can be run in one thread and the test
  40. harness is run in another thread.</li>
  41. </ul>
  42. <p>Care must be taken when using multithreading to ensure the tasks within the
  43. threads do not interact. For example, two javac compile tasks which write
  44. classes into the same destination directory may interact where one tries to
  45. read a class for dependency information while the other task is writing the
  46. class file. Be sure to avoid these types of interactions within a
  47. &lt;parallel&gt; task</p>
  48. <p>The parallel task has no attributes and does not support any nested
  49. elements apart from Ant tasks. Any valid Ant task may be embedded within a
  50. parallel task, including other parallel tasks.</p>
  51. <p>Note that while the tasks within the parallel task are being run, the main
  52. thread will be blocked waiting for all the child threads to complete.</p>
  53. <p>If any of the tasks within the &lt;parallel&gt; task fails, the remaining
  54. tasks in other threads will continue to run until all threads have completed.
  55. In this situation, the parallel task will also fail.</p>
  56. <p>The parallel task may be combined with the <a href="sequential.html">
  57. sequential</a> task to define sequences of tasks to be executed on each thread
  58. within the parallel block</p>
  59. <p>The threadCount attribute can be used to place a maximum number of available
  60. threads for the execution. When not present all child tasks will be executed at
  61. once. When present then the maximum number of concurrently executing tasks will
  62. not exceed the number of threads specified. Furthermore, each task will be
  63. started in the order they are given. But no guarantee is made as to the speed
  64. of execution or the order of completion of the tasks, only that each will be
  65. started before the next.<p>
  66. <p>If you are using J2RE 1.4 or later you can also use the threadsPerProcessor
  67. and the number of available threads will be the stated multiple of the number of
  68. processors (there is no affinity to a particular processor however). This will
  69. override the value in threadCount. If threadsPerProcessor is specified using
  70. any version prior to 1.4 then the value in threadCount will be used as is.</p>
  71. <p>When using threadCount and threadsPerProcessor care should be taken to insure
  72. that the build does not deadlock. This can be caused by tasks such as waitFor
  73. takeing up all available threads before the tasks that would unlock the waitfor
  74. would occur. This is not a repalcement for Java Language level thread
  75. semantics and is best used for "embarasingly parallel" tasks.</p>
  76. <h3>Examples</h3>
  77. <pre>
  78. &lt;parallel&gt;
  79. &lt;wlrun ... &gt;
  80. &lt;sequential&gt;
  81. &lt;sleep seconds=&quot;30&quot;/&gt;
  82. &lt;junit ... &gt;
  83. &lt;wlstop/&gt;
  84. &lt;/sequential&gt;
  85. &lt;/parallel&gt;
  86. </pre>
  87. <p>This example represents a typical pattern for testing a server application.
  88. In one thread the server is started (the wlrun task). The other thread consists
  89. of a three tasks which are performed in sequence. The sleep task is used to
  90. give the server time to come up. Another task which is capable of validating
  91. that the server is available could be used in place of the sleep task. The
  92. test harness is then run. Once the tests are complete, the server is stopped
  93. (using wlstop in this example), allowing both threads to complete. The
  94. parallel task will also complete at this time and the build will then
  95. continue.</p>
  96. <pre>
  97. &lt;parallel&gt;
  98. &lt;javac ...&gt; &lt;!-- compiler servlet code --&gt;
  99. &lt;wljspc ...&gt; &lt;!-- precompile JSPs --&gt;
  100. &lt;/parallel&gt;
  101. </pre>
  102. <p>This example shows two independent tasks being run to achieve better
  103. resource utilization during the build. In this instance, some servlets are being
  104. compiled in one thead and a set of JSPs is being precompiled in another. As
  105. noted above, you need to be careful that the two tasks are independent, both in
  106. terms of their dependencies and in terms of their potential interactions in
  107. Ant's external environment.</p>
  108. <pre>
  109. &lt;parallel threadCount='4'&gt;
  110. &lt;ant target='TargetThatConsumesLotsOfCPUTimeAndMemory'&gt;
  111. &lt;param name='file' value='one.txt'/&gt;
  112. &lt;/ant&gt;
  113. &lt;ant target='TargetThatConsumesLotsOfCPUTimeAndMemory'&gt;
  114. &lt;param name='file' value='two.txt'/&gt;
  115. &lt;/ant&gt;
  116. &lt;ant target='TargetThatConsumesLotsOfCPUTimeAndMemory'&gt;
  117. &lt;param name='file' value='three.txt'/&gt;
  118. &lt;/ant&gt;
  119. &lt;!-- repeated about 40 times --&gt;
  120. &lt;/parallel&gt;
  121. </pre>
  122. <p>This example represents a typical need for use of the threadCount and
  123. threadsPerProcessor attributes. Spinning up all 40 of those tasks could cripple
  124. the JVM for memory and the CPU for available time. By limiting the number of
  125. concurrent executions you can get the task done in about the same assuming
  126. infinite memory time without needing infinite memory. This is also a good
  127. candidiate for use of threadCount (and possibly threadsPerProcessor) because
  128. each task (in this hypothetical case) is independent and has no dependencies on
  129. the other tasks.</p>
  130. <hr>
  131. <p align="center">Copyright &copy; 2001-2003 Apache Software Foundation. All rights
  132. Reserved.</p>
  133. </body>
  134. </html>