You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by mw...@apache.org on 2002/12/10 06:32:33 UTC

synchronization problem with listeners in Hierarchy?

So, this is some of the code that I added to Hierarchy:

  /**
    Requests that a configuration changed event be sent to any registered
    {@link LoggerRepositoryEventListener}.
    @since 1.3*/
  public void fireConfigurationChangedEvent() {
    if(repositoryEventListeners != null) {
      int size = repositoryEventListeners.size();
      LoggerRepositoryEventListener listener;
      for(int i = 0; i < size; i++) {
        listener = (LoggerRepositoryEventListener)
          repositoryEventListeners.elementAt(i);
        listener.configurationChangedEvent(this);
      }
    }
  }

But there are also methods that allow one to remove a given listener from
the hierarchy.  So, there will be a problem here if a listener is removed
while in the for loop to reportiting an event.

What would be the best way to deal with it?  Just add a catch for the index
out of bounds exception or to synchronize on the listener vectors?

-Mark


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: synchronization problem with listeners in Hierarchy?

Posted by Ceki Gülcü <ce...@qos.ch>.
Mark,

I am thrilled with the recent commits. I'd like to re-read them carefully 
before making further comments. As for looping on repositoryEventListeners, 
getting an enumeration on the listeners vectors might be safe. Nicko 
Cadell's approach of an array copy should be equally safe if not safer.

At 21:32 09.12.2002 -0800, mwomack@apache.org wrote:
>So, this is some of the code that I added to Hierarchy:
>
>   /**
>     Requests that a configuration changed event be sent to any registered
>     {@link LoggerRepositoryEventListener}.
>     @since 1.3*/
>   public void fireConfigurationChangedEvent() {
>     if(repositoryEventListeners != null) {
>       int size = repositoryEventListeners.size();
>       LoggerRepositoryEventListener listener;
>       for(int i = 0; i < size; i++) {
>         listener = (LoggerRepositoryEventListener)
>           repositoryEventListeners.elementAt(i);
>         listener.configurationChangedEvent(this);
>       }
>     }
>   }
>
>But there are also methods that allow one to remove a given listener from
>the hierarchy.  So, there will be a problem here if a listener is removed
>while in the for loop to reportiting an event.
>
>What would be the best way to deal with it?  Just add a catch for the index
>out of bounds exception or to synchronize on the listener vectors?

A copy of the list is better for several reasons.

>-Mark

--
Ceki



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>