Very clever! Does that work with removing a listener as well? I mean, what
about the unlikely event that a listener decides to remove itself from a
separate thread while an event is being fired from another thread, and thus
the list of listener traversed? Assume the listener being removed as not
been fired yet. I'd need to synchronize both removeListener and fireEvent,
no??? Just curious. I'm no threading expert for sure ;-) --DD

> -----Original Message-----
> From: Sylvain Wallez [mailto:[EMAIL PROTECTED]
> Sent: Monday, August 18, 2003 3:18 AM
> To: dev@cocoon.apache.org
> Cc: [EMAIL PROTECTED]
> Subject: Re: [OT] Build Time
> 
> To solve this problem, you may consider the fact that listeners are more
> often fired than changed, and therefore use a different pattern, which
> does the cloning and synchronisation when the listener list has to be
> changed, and not when it has to be traversed.
> 
> private List listenerers;
> 
> public void fireEvent(Event ev) {
>   Iterator iter = this.listeners.iterator();
>   while(iter.hasNext()) {
>     ((EventListener)iter.next()).fireEvent(ev);
>   }
> }
> 
> public void synchronized addListener(EventListener evl) {
>   // Make a local copy on which we will act : it avoids
>   // ConcurrentModificationException if someone is executing fireEvent()
>   List newListeners = this.listeners.clone();
>   newListeners.add(evl);
>   // and replace the listener list
>   this.listeners = newListeners;
> }
> 
> Sylvain

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to