Browse Source

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
master
Conor MacNeill 22 years ago
parent
commit
55a7cf2fa5
2 changed files with 27 additions and 9 deletions
  1. +18
    -5
      src/main/org/apache/tools/ant/Project.java
  2. +9
    -4
      src/main/org/apache/tools/ant/taskdefs/Parallel.java

+ 18
- 5
src/main/org/apache/tools/ant/Project.java View File

@@ -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 (<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 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 (<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 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 {


+ 9
- 4
src/main/org/apache/tools/ant/taskdefs/Parallel.java View File

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


Loading…
Cancel
Save