How do I use the parallel task?
Author: Deron Eriksson
Description: This Ant tutorial describes the use of the parallel task for running multiple threads at once
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
(Continued from page 1) As additional examples, let's consider the following two targets. The 'open-one-notepad-after-another' target opens one notepad window for three seconds, and then after it closes it opens another notepad window for three seconds. This is because the exec tasks execute one after the other in one thread. On the other hand, with the 'open-two-notepads-at-once' target, two notepad windows are opened at the same time for three seconds each, since each exec task is executed in its own thread. <target name="open-one-notepad-after-another"> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </target> <target name="open-two-notepads-at-once"> <parallel> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> </target> As further examples of the parallel task, consider the following. The 'open-two-notepads-and-then-two-notepads' task opens two notepad windows at the same time for three seconds each and then closes them. After that, it opens two more notepad windows for three seconds each and then closes them both. This parallel task utilizes the 'threadcount' attribute, which limits the number of concurrent threads to be 2 in this example. In the second 'open-two-notepads-and-then-two-notepads-2' target, we achieve similar results by running one parallel task after another, both of which open two notepad windows for three seconds and then close them. <target name="open-two-notepads-and-then-two-notepads"> <parallel threadcount="2"> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> </target> <target name="open-two-notepads-and-then-two-notepads-2"> <parallel> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> <parallel> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> </target> The full build.xml script used in these examples is given below: build.xml<?xml version="1.0" encoding="UTF-8"?> <project name="ant-demo" default="" basedir="."> <target name="output-1-2-kill-notepad-output-3-4"> <echo message="1" /> <echo message="2" /> <exec executable="notepad.exe" timeout="3000" /> <echo message="3" /> <echo message="4" /> </target> <target name="output-1-2-3-kill-notepad-output-4"> <echo message="1" /> <parallel> <echo message="2" /> <exec executable="notepad.exe" timeout="3000" /> <echo message="3" /> </parallel> <echo message="4" /> </target> <target name="open-one-notepad-after-another"> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </target> <target name="open-two-notepads-at-once"> <parallel> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> </target> <target name="open-two-notepads-and-then-two-notepads"> <parallel threadcount="2"> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> </target> <target name="open-two-notepads-and-then-two-notepads-2"> <parallel> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> <parallel> <exec executable="notepad.exe" timeout="3000" /> <exec executable="notepad.exe" timeout="3000" /> </parallel> </target> </project> |