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


Page:    1 2 >

Ant's 'parallel' task gives you the ability to run the tasks within the parallel task in their own threads. Why is this useful? As an example, consider the situation where you use an AntSW script to deploy a warW file to an environment with multiple application servers. If you only had one thread, you would need to deploy to the application servers one-at-a-time. If you're running an application server like OC4J, this may require tasks such as stopping, purging, and starting each application server. If the 'stop-purge-start' required three minutes for each application server and you had 4 application servers, it would take 12 minutes to stop and start all the app servers.

However, if you run each 'stop-purge-start' Ant task in parallel with its own thread, it would only take 3 minutes to stop and start all the app servers. It would be silly to stop and start the app servers one after the other when you can start and stop them all at the same time. If you deploy a hundred times in year, you can imagine what a timesaver this is.

Let's look at some examples. The 'output-1-2-kill-notepad-output-3-4' example is shown below. It doesn't do any parallel threads.

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

The 'output-1-2-kill-notepad-output-3-4' target produces the output shown below. It outputs '1' and '2' to the console, opens a notepad window for three seconds, and then outputs '3' and '4' to the console.

Buildfile: C:\projects\workspace\ant-demo\build.xml
output-1-2-kill-notepad-output-3-4:
     [echo] 1
     [echo] 2
     [exec] Timeout: killed the sub-process
     [exec] Result: 1
     [echo] 3
     [echo] 4
BUILD SUCCESSFUL
Total time: 3 seconds

The next example, 'output-1-2-3-kill-notepad-output-4' is our first example of the parallel task.

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

The output from 'output-1-2-3-kill-notepad-output-4' is shown below. It outputs '1' to the console. Then it moves on to the parallel task, and executes each of the three parallel tasks in its own thread. It outputs '2' to the console, opens a notepad window (which stays open for three seconds), and outputs '3' to the console. Notice that the '3' echo message doesn't need to wait for the notepad window to close before it is output, since it is executed in its own thread. As a result, '3' is output before the message saying that the notepad window has been killed. Also, notice that the '4' message isn't output to the console until after the last parallel task is done (ie, the notepad window which takes 3 seconds to close).

Buildfile: C:\projects\workspace\ant-demo\build.xml
output-1-2-3-kill-notepad-output-4:
     [echo] 1
     [echo] 2
     [echo] 3
     [exec] Timeout: killed the sub-process
     [exec] Result: 1
     [echo] 4
BUILD SUCCESSFUL
Total time: 3 seconds

(Continued on page 2)

Page:    1 2 >