diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index c45d29e92..d913a8dc7 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -176,10 +176,11 @@ public class Project implements ResourceFactory { private ClassLoader coreLoader = null; /** Records the latest task to be executed on a thread. */ - private Map/**/ threadTasks = Collections.synchronizedMap(new WeakHashMap()); + private final Map/**/ threadTasks = + Collections.synchronizedMap(new WeakHashMap()); /** Records the latest task to be executed on a thread group. */ - private Map/**/ threadGroupTasks + private final Map/**/ threadGroupTasks = Collections.synchronizedMap(new WeakHashMap()); /** @@ -2271,13 +2272,15 @@ public class Project implements ResourceFactory { * @param task the task to be registered. * @since Ant 1.5 */ - 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()); + public void registerThreadTask(Thread thread, Task task) { + synchronized(threadTasks) { + if (task != null) { + threadTasks.put(thread, task); + threadGroupTasks.put(thread.getThreadGroup(), task); + } else { + threadTasks.remove(thread); + threadGroupTasks.remove(thread.getThreadGroup()); + } } } @@ -2289,15 +2292,17 @@ public class Project implements ResourceFactory { * null if no task is registered. */ public Task getThreadTask(Thread 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(); + synchronized(threadTasks) { + 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; } - return task; }