From a65cadb437afdc85fcc77d9c58293c66e456ff46 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 19 Jun 2008 08:42:59 +0000 Subject: [PATCH] Add a separate lock for the BuildListener collection git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@669426 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/org/apache/tools/ant/Project.java | 37 ++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index d913a8dc7..c64d6a781 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -166,6 +166,9 @@ public class Project implements ResourceFactory { /** Project base directory. */ private File baseDir; + /** lock object used when adding/removing listeners */ + private final Object listenersLock = new Object(); + /** List of listeners to notify of build events. */ private Vector listeners = new Vector(); @@ -378,16 +381,17 @@ public class Project implements ResourceFactory { * @param listener The listener to add to the list. * Must not be null. */ - public synchronized void addBuildListener(BuildListener listener) { - // If the listeners already has this listener, do nothing - if (listeners.contains(listener)) { - return; + public void addBuildListener(BuildListener listener) { + synchronized (listenersLock) { + // If the listeners already has this listener, do nothing + if (listeners.contains(listener)) { + return; + } + // copy on write semantics + Vector newListeners = getBuildListeners(); + newListeners.addElement(listener); + listeners = newListeners; } - // create a new Vector to avoid ConcurrentModificationExc when - // the listeners get added/removed while we are in fire - Vector newListeners = getBuildListeners(); - newListeners.addElement(listener); - listeners = newListeners; } /** @@ -397,12 +401,13 @@ public class Project implements ResourceFactory { * @param listener The listener to remove from the list. * Should not be null. */ - public synchronized void removeBuildListener(BuildListener listener) { - // create a new Vector to avoid ConcurrentModificationExc when - // the listeners get added/removed while we are in fire - Vector newListeners = getBuildListeners(); - newListeners.removeElement(listener); - listeners = newListeners; + public void removeBuildListener(BuildListener listener) { + synchronized (listenersLock) { + // copy on write semantics + Vector newListeners = getBuildListeners(); + newListeners.removeElement(listener); + listeners = newListeners; + } } /** @@ -2152,7 +2157,7 @@ public class Project implements ResourceFactory { * * @see http://marc.theaimsgroup.com/?t=110538624200006&r=1&w=2 * - * We now (Ant 1.7 and 1.6.3) simply swallow the message. + * We now (Ant 1.6.3 and later) simply swallow the message. */ return; }