Author: bodewig Date: Thu Jun 19 01:42:59 2008 New Revision: 669426 URL: http://svn.apache.org/viewvc?rev=669426&view=rev Log: Add a separate lock for the BuildListener collection
Modified: ant/core/trunk/src/main/org/apache/tools/ant/Project.java Modified: ant/core/trunk/src/main/org/apache/tools/ant/Project.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/Project.java?rev=669426&r1=669425&r2=669426&view=diff ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/Project.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/Project.java Thu Jun 19 01:42:59 2008 @@ -166,6 +166,9 @@ /** 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 @@ * @param listener The listener to add to the list. * Must not be <code>null</code>. */ - 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 @@ * @param listener The listener to remove from the list. * Should not be <code>null</code>. */ - 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 @@ * * @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; }