Top Description Inners Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class Parallel

extends Task
implements TaskContainer
Class Inheritance
All Implemented Interfaces
org.apache.tools.ant.TaskContainer
Imports
java.util.ArrayList, .List, .Vector, org.apache.tools.ant.BuildException, .ExitStatusException, .Location, .Task, .TaskContainer, org.apache.tools.ant.property.LocalProperties

Executes the contained tasks in separate threads, continuing once all are completed.

New behavior allows for the ant script to specify a maximum number of threads that will be executed in parallel. One should be very careful about using the waitFor task when specifying threadCount as it can cause deadlocks if the number of threads is too small or if one of the nested tasks fails to execute completely. The task selection algorithm will insure that the tasks listed before a task have started before that task is started, but it will not insure a successful completion of those tasks or that those tasks will finish first (i.e. it's a classic race condition).

Since
Ant 1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Parallel.TaskList

Class which holds a list of tasks to execute

private class
Parallel.TaskRunnable

thread that execs a task

Field Summary

Modifier and TypeField and Description
private Parallel.TaskList
daemonTasks

The daemon task list if any

private StringBuffer
exceptionMessage

Accumulation of exceptions messages from all nested tasks

private boolean
failOnAny

Indicates whether failure of any of the nested tasks should end execution

private Throwable
firstException

The first exception encountered

private Integer
firstExitStatus

The status of the first ExitStatusException.

private Location
firstLocation

The location of the first exception

private Vector<Task>
nestedTasks

Collection holding the nested tasks

private static final int
private int
numExceptions

Number of exceptions from nested tasks

private int
numThreads

Total number of threads to run

private int
numThreadsPerProcessor

Total number of threads per processor to run.

private final Object
semaphore

Semaphore to notify of completed threads

private volatile boolean
stillRunning

Indicates threads are still running and new threads can be issued

private boolean
timedOut

Indicates that the execution timedout

private long
timeout

The timeout period in milliseconds

Inherited from org.apache.tools.ant.Task:
targettaskNametaskTypewrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addDaemons(Parallel.TaskList
The tasks to be executed as daemon.
daemonTasks
)

Add a group of daemon threads

public void
addTask(Task
Nested task to be executed in parallel
nestedTask
)

Implements org.apache.tools.ant.TaskContainer.addTask.

Add a nested task to execute in parallel.
public void
execute()

Overrides org.apache.tools.ant.Task.execute.

Execute the parallel tasks
private void
killAll(Parallel.TaskRunnable[]
The list of tasks that may currently be running.
running
)

Doesn't do anything if all threads where already gone, else it tries to interrupt the threads 100 times.

private void
public void
setFailOnAny(boolean
if true any nested task failure causes parallel to complete.
failOnAny
)

Control whether a failure in a nested task halts execution.

public void
setPollInterval(int
New value of property pollInterval.
pollInterval
)

Interval to poll for completed threads when threadCount or threadsPerProcessor is specified.

public void
setThreadCount(int
total number of threads.
numThreads
)

Statically determine the maximum number of tasks to execute simultaneously.

public void
setThreadsPerProcessor(int
Number of threads to create per available processor.
numThreadsPerProcessor
)

Dynamically generates the number of threads to execute based on the number of available processors (via java.lang.Runtime.availableProcessors()).

public void
setTimeout(long
timeout in milliseconds.
timeout
)

Sets the timeout on this set of tasks.

private void
spinThreads()

Spin up required threads with a maximum number active at any given time.

private void
updateThreadCounts()

Determine the number of threads based on the number of processors

Inherited from org.apache.tools.ant.Task:
bindToOwnergetOwningTargetgetRuntimeConfigurableWrappergetTaskNamegetTaskTypegetWrapperhandleErrorFlushhandleErrorOutputhandleFlushhandleInputhandleOutputinitisInvalidloglogloglogmaybeConfigureperformreconfiguresetOwningTargetsetRuntimeConfigurableWrappersetTaskNamesetTaskType

Field Detail

daemonTasksback to summary
private Parallel.TaskList daemonTasks

The daemon task list if any

exceptionMessageback to summary
private StringBuffer exceptionMessage

Accumulation of exceptions messages from all nested tasks

failOnAnyback to summary
private boolean failOnAny

Indicates whether failure of any of the nested tasks should end execution

firstExceptionback to summary
private Throwable firstException

The first exception encountered

firstExitStatusback to summary
private Integer firstExitStatus

The status of the first ExitStatusException.

firstLocationback to summary
private Location firstLocation

The location of the first exception

nestedTasksback to summary
private Vector<Task> nestedTasks

Collection holding the nested tasks

NUMBER_TRIESback to summary
private static final int NUMBER_TRIES
numExceptionsback to summary
private int numExceptions

Number of exceptions from nested tasks

numThreadsback to summary
private int numThreads

Total number of threads to run

numThreadsPerProcessorback to summary
private int numThreadsPerProcessor

Total number of threads per processor to run.

semaphoreback to summary
private final Object semaphore

Semaphore to notify of completed threads

stillRunningback to summary
private volatile boolean stillRunning

Indicates threads are still running and new threads can be issued

timedOutback to summary
private boolean timedOut

Indicates that the execution timedout

timeoutback to summary
private long timeout

The timeout period in milliseconds

Constructor Detail

Parallelback to summary
public Parallel()

Method Detail

addDaemonsback to summary
public void addDaemons(Parallel.TaskList daemonTasks)

Add a group of daemon threads

Parameters
daemonTasks:Parallel.TaskList

The tasks to be executed as daemon.

addTaskback to summary
public void addTask(Task nestedTask)

Implements org.apache.tools.ant.TaskContainer.addTask.

Add a nested task to execute in parallel.

Parameters
nestedTask:Task

Nested task to be executed in parallel

Annotations
@Override
executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

Execute the parallel tasks

Annotations
@Override
Exceptions
BuildException:
if any of the threads failed.
killAllback to summary
private void killAll(Parallel.TaskRunnable[] running)

Doesn't do anything if all threads where already gone, else it tries to interrupt the threads 100 times.

Parameters
running:Parallel.TaskRunnable[]

The list of tasks that may currently be running.

processExceptionsback to summary
private void processExceptions(Parallel.TaskRunnable[] runnables)
setFailOnAnyback to summary
public void setFailOnAny(boolean failOnAny)

Control whether a failure in a nested task halts execution. Note that the task will complete but existing threads will continue to run - they are not stopped

Parameters
failOnAny:boolean

if true any nested task failure causes parallel to complete.

setPollIntervalback to summary
public void setPollInterval(int pollInterval)

Interval to poll for completed threads when threadCount or threadsPerProcessor is specified. Integer in milliseconds.; optional

Parameters
pollInterval:int

New value of property pollInterval.

setThreadCountback to summary
public void setThreadCount(int numThreads)

Statically determine the maximum number of tasks to execute simultaneously. If there are less tasks than threads then all will be executed at once, if there are more then only threadCount tasks will be executed at one time. If threadsPerProcessor is set then this value is ignored.; optional

Parameters
numThreads:int

total number of threads.

setThreadsPerProcessorback to summary
public void setThreadsPerProcessor(int numThreadsPerProcessor)

Dynamically generates the number of threads to execute based on the number of available processors (via java.lang.Runtime.availableProcessors()). Will overwrite the value set in threadCount; optional

Parameters
numThreadsPerProcessor:int

Number of threads to create per available processor.

setTimeoutback to summary
public void setTimeout(long timeout)

Sets the timeout on this set of tasks. If the timeout is reached before the other threads complete, the execution of this task completes with an exception. Note that existing threads continue to run.

Parameters
timeout:long

timeout in milliseconds.

spinThreadsback to summary
private void spinThreads() throws BuildException

Spin up required threads with a maximum number active at any given time.

Exceptions
BuildException:
if any of the threads failed.
updateThreadCountsback to summary
private void updateThreadCounts()

Determine the number of threads based on the number of processors

org.apache.tools.ant.taskdefs back to summary

public Class Parallel.TaskList

extends Object
implements TaskContainer
Class Inheritance
All Implemented Interfaces
org.apache.tools.ant.TaskContainer

Class which holds a list of tasks to execute

Field Summary

Modifier and TypeField and Description
private List<Task>
tasks

Collection holding the nested tasks

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addTask(Task
Nested task to be executed in parallel. must not be null.
nestedTask
)

Implements org.apache.tools.ant.TaskContainer.addTask.

Add a nested task to execute in parallel (asynchronously).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

tasksback to summary
private List<Task> tasks

Collection holding the nested tasks

Constructor Detail

TaskListback to summary
public TaskList()

Method Detail

addTaskback to summary
public void addTask(Task nestedTask)

Implements org.apache.tools.ant.TaskContainer.addTask.

Add a nested task to execute in parallel (asynchronously).

Parameters
nestedTask:Task

Nested task to be executed in parallel. must not be null.

Annotations
@Override
org.apache.tools.ant.taskdefs back to summary

private Class Parallel.TaskRunnable

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

thread that execs a task

Field Summary

Modifier and TypeField and Description
private Throwable
private boolean
private Task
private volatile Thread

Constructor Summary

AccessConstructor and Description
pack-priv
TaskRunnable(Task
the Task to be executed in a separate thread
task
)

Construct a new TaskRunnable.

Method Summary

Modifier and TypeMethod and Description
public Throwable

Returns:

an exception or null for no exception/not yet finished
getException
()

get any exception that got thrown during execution;

pack-priv void
pack-priv boolean

Returns:

Returns true when the task is finished.
isFinished
()

Provides the indicator that the task has been finished.

public void
run()

Implements java.lang.Runnable.run.

Executes the task within a thread and takes care about Exceptions raised within the task.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

exceptionback to summary
private Throwable exception
finishedback to summary
private boolean finished
taskback to summary
private Task task
threadback to summary
private volatile Thread thread

Constructor Detail

TaskRunnableback to summary
pack-priv TaskRunnable(Task task)

Construct a new TaskRunnable.

Parameters
task:Task

the Task to be executed in a separate thread

Method Detail

getExceptionback to summary
public Throwable getException()

get any exception that got thrown during execution;

Returns:Throwable

an exception or null for no exception/not yet finished

interruptback to summary
pack-priv void interrupt()
isFinishedback to summary
pack-priv boolean isFinished()

Provides the indicator that the task has been finished.

Returns:boolean

Returns true when the task is finished.

runback to summary
public void run()

Implements java.lang.Runnable.run.

Executes the task within a thread and takes care about Exceptions raised within the task.

Annotations
@Override