From 55a7cf2fa592bc0e96266ddd6bdb904a9a7526ba Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Mon, 10 Feb 2003 12:36:46 +0000 Subject: [PATCH] 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 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274035 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/org/apache/tools/ant/Project.java | 23 +++++++++++++++---- .../apache/tools/ant/taskdefs/Parallel.java | 13 +++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index 65581225b..39d95c8a8 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -205,6 +205,9 @@ public class Project { /** 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 @@ public class Project { * or information (false). */ 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 class Project { */ 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 @@ public class Project { * or information (false). */ 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 class Project { 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,10 +2131,18 @@ public class Project { * 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; } - + // Should move to a separate public class - and have API to add // listeners, etc. private static class AntRefTable extends Hashtable { diff --git a/src/main/org/apache/tools/ant/taskdefs/Parallel.java b/src/main/org/apache/tools/ant/taskdefs/Parallel.java index cb5b30d06..51951fc73 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Parallel.java +++ b/src/main/org/apache/tools/ant/taskdefs/Parallel.java @@ -94,12 +94,17 @@ public class Parallel extends Task * 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 @@ public class Parallel extends Task 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 @@ public class Parallel extends Task /** * thread that execs a task */ - class TaskThread extends Thread { + private static class TaskThread implements Runnable { private Throwable exception; private Task task; private int taskNumber;