conor 2003/02/10 04:36:46
Modified: src/main/org/apache/tools/ant Project.java
src/main/org/apache/tools/ant/taskdefs Parallel.java
Log:
Use ThreadGroups to link tasks to any threads they create
Make sure parallel creates a separate thread group for each
thread it spawns
PR: 7980
Revision Changes Path
1.130 +19 -6 jakarta-ant/src/main/org/apache/tools/ant/Project.java
Index: Project.java
===================================================================
RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.129
retrieving revision 1.130
diff -u -w -u -r1.129 -r1.130
--- Project.java 9 Feb 2003 08:09:43 -0000 1.129
+++ Project.java 10 Feb 2003 12:36:45 -0000 1.130
@@ -205,6 +205,9 @@
/** Records the latest task to be executed on a thread (Thread to Task).
*/
private Hashtable threadTasks = new Hashtable();
+ /** Records the latest task to be executed on a thread Group. */
+ private Hashtable threadGroupTasks = new Hashtable();
+
/**
* Called to handle any input requests.
*/
@@ -1271,7 +1274,7 @@
* or information (<code>false</code>).
*/
public void demuxOutput(String line, boolean isError) {
- Task task = (Task) threadTasks.get(Thread.currentThread());
+ Task task = getThreadTask(Thread.currentThread());
if (task == null) {
fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO);
} else {
@@ -1319,7 +1322,7 @@
*/
public int demuxInput(byte[] buffer, int offset, int length)
throws IOException {
- Task task = (Task) threadTasks.get(Thread.currentThread());
+ Task task = getThreadTask(Thread.currentThread());
if (task == null) {
return defaultInput(buffer, offset, length);
} else {
@@ -1339,7 +1342,7 @@
* or information (<code>false</code>).
*/
public void demuxFlush(String line, boolean isError) {
- Task task = (Task) threadTasks.get(Thread.currentThread());
+ Task task = getThreadTask(Thread.currentThread());
if (task == null) {
fireMessageLogged(this, line, isError ? MSG_ERR : MSG_INFO);
} else {
@@ -2113,8 +2116,10 @@
public synchronized void registerThreadTask(Thread thread, Task task) {
if (task != null) {
threadTasks.put(thread, task);
+ threadGroupTasks.put(thread.getThreadGroup(), task);
} else {
threadTasks.remove(thread);
+ threadGroupTasks.remove(thread.getThreadGroup());
}
}
@@ -2126,7 +2131,15 @@
* null if no task is registered.
*/
public Task getThreadTask(Thread thread) {
- return (Task) threadTasks.get(thread);
+ Task task = (Task) threadTasks.get(thread);
+ if (task == null) {
+ ThreadGroup group = thread.getThreadGroup();
+ while (task == null && group != null) {
+ task = (Task) threadGroupTasks.get(group);
+ group = group.getParent();
+ }
+ }
+ return task;
}
1.13 +9 -4
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Parallel.java
Index: Parallel.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Parallel.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -w -u -r1.12 -r1.13
--- Parallel.java 25 Jul 2002 15:21:05 -0000 1.12
+++ Parallel.java 10 Feb 2003 12:36:46 -0000 1.13
@@ -94,12 +94,17 @@
* execute the wait status.
*/
public void execute() throws BuildException {
- TaskThread[] threads = new TaskThread[nestedTasks.size()];
+ int numTasks = nestedTasks.size();
+ Thread[] threads = new Thread[numTasks];
+ TaskThread[] taskThreads = new TaskThread[numTasks];
int threadNumber = 0;
for (Enumeration e = nestedTasks.elements(); e.hasMoreElements();
threadNumber++) {
Task nestedTask = (Task) e.nextElement();
- threads[threadNumber] = new TaskThread(threadNumber, nestedTask);
+ ThreadGroup group = new ThreadGroup("parallel");
+ TaskThread taskThread = new TaskThread(threadNumber, nestedTask);
+ taskThreads[threadNumber] = taskThread;
+ threads[threadNumber] = new Thread(group, taskThread);
}
// now start all threads
@@ -122,7 +127,7 @@
Throwable firstException = null;
Location firstLocation = Location.UNKNOWN_LOCATION;;
for (int i = 0; i < threads.length; ++i) {
- Throwable t = threads[i].getException();
+ Throwable t = taskThreads[i].getException();
if (t != null) {
numExceptions++;
if (firstException == null) {
@@ -152,7 +157,7 @@
/**
* thread that execs a task
*/
- class TaskThread extends Thread {
+ private static class TaskThread implements Runnable {
private Throwable exception;
private Task task;
private int taskNumber;